Vector in R Language

A vector in R is a set of numbers. A vector can be considered as a single column or a single row of a spreadsheet. The following examples are numbers that are not technically “vectors”. It is because these vectors are not in a column/row structure, however, they are ordered. These vectors can be referred to by index.

Creating Vector in R

# Creating a vector with the c function

c(1, 4, 6, 7, 9)

c(1:5, 10)
Creating Vector in R Language

A vector in R language can be created using seq() function, it generates a series of numbers.

# Create a vector using seq() function

seq(1, 10, by = 2)
seq(0, 50, length = 11)
seq(1, 50, length = 11)
Creating Vector in R using seq() Function

The vector can be created in R using the colon (:) operator. Following are the examples

# Create vector using : operator

1:10

## Output
[1]  1  2  3  4  5  6  7  8  9 10

5:1

## Output
[1] 5 4 3 2 1

The non-integer sequences can also be created in R Language.

# non-integer sequences
seq(0, 100*pi, by = pi)
Non integer vector in R

One can assign a vector to a variable using the assignment operator (<-) or equal symbol (=). The examples are:

a <- 1:5
b <- seq(15, 3, length=5)
c <- a * b

There are a lot of built-in functions that can be used to perform different computations on vectors. For example,

a <- 1:5

# compute the total of elements of a vector
sum(a)

## Output
15

# product of elements of a vector
prod(a)

## Output
120

# average of the vector
mean(a)

## Output
3

# standard deviation and variance of a vector
sd(a)

## Output 
1.581139

var(a)

## Output
2.5

One can extract the elements of a vector by using square brackets and the index of the component of the vector.

V <- seq(0, 100, by = 10)
V[] # gives all the elements of the vector

## Output
[1]   0  10  20  30  40  50  60  70  80  90 100

V[5] # 5th elements from vector z

## Output
[1] 40

V[c(2, 4, 6, 8)] #2nd, 4th, th, and 8th element

## Output
[1] 10 30 50 70

V[-c(2, 4, 6, 8)] # elements except 2nd, 4th, 6th, and 8th element

## Output
[1]   0  20  40  60  80  90 100

The specific / required elements of a vector can be updated

V[c(2, 4)] <- c(500, 600) # the second and 4th element is updated to 500 and 600
Updating vector elements in R

https://itfeature.com

https://gmstat.com

The important points about vectors in R language are:

  • Data Types: Vectors can hold logical, integer, double, character, complex, or raw data.
  • Creation: Use the c() function to combine elements into a vector.
  • Accessing Elements: Use indexing (square brackets) to access individual elements.
  • Vector Operations: Perform arithmetic, logical, and comparison operations on vectors.
  • Vectorization: R excels at vectorized operations, making calculations efficient.

Important Data Frame Questions (2024)

The post contains Data frame Questions and Answers. A data frame in R is a fundamental data structure used to store and organize tabular data. A Data Frame is like a spreadsheet with rows and columns, but more flexible in data types.

Merging Data Frames inR

Question 1: How two data frames can be merged in R language?

Answer: Data frames in the R language can be merged manually using the column bind function cbind() or by using the merge() function on common rows or columns.

Question 2: What is the difference between a data frame and a matrix in R?

Answer: A Data frame can contain heterogeneous inputs while a matrix cannot. In a matrix only similar data types (say either numeric or symbols) can be stored whereas in a data frame, there can be different data types like characters, integers, or other data frames. In short columns of a matrix have the same data type while different columns of a data frame can have different data types.

Dropping Variables Using Indices

Question 3: How will you drop variables using indices in a data frame?

Answer: Consider the data frame the following data frame

df <- data.frame(v1 = c(1:5),
                 v2 = c(2:6),
                 v3 = c(3:7),
                 v4 = c(4:8))
df

# output
  v1 v2 v3 v4
1  1  2  3  4
2  2  3  4  5
3  3  4  5  6
4  4  5  6  7
5  5  6  7  8
Data Frame Questions and Answers

Suppose we want to drop variables $v2$ & $v3$, the variables $v2$ and $v3$ can be dropped using negative indicies as follows:

df1 <- df[-c(2, 3)]
df1

#output
  v1 v4
1  1  4
2  2  5
3  3  6
4  4  7
5  5  8

One can do the same by using the positive indexes.

df2 <- df[c(1, 4)]
df2

#output
  v1 v4
1  1  4
2  2  5
3  3  6
4  4  7
5  5  8

Merging Data Frame in R Language

Question 4: How two Data Frames can be merged in the R programming language?

Answer: The merge() function in R is used to combine two data frames and it identifies common rows or columns between the 2 data frames. The merge() function finds the intersection between two different sets of data. The merge() function in R language takes a long list of arguments as follows

The syntax for using the merge() function in R language:

 merge (x, y, by.x, by.y, all.x  or all.y or all )
  • $X$ represents the first data frame.
  • $Y$ represents the second data frame.
  • $by.X$ Variable name in dataframe $X$ that is common in $Y$.
  • $by.Y$ Variable name in dataframe $Y$ that is common in $X$.
  • $all.x$ It is a logical value that specifies the type of merge. The $all.X$ should be set to TRUE if we want all the observations from data frame $X$. This results in Left Join.
  • $all.y$ It is a logical value that specifies the type of merge. The $all.y$ should be set to TRUE if we want all the observations from data frame $Y$. This results in Right Join.
  • $all$ The default value for this is set to FALSE which means that only matching rows are returned resulting in an Inner join. This should be set to true if you want all the observations from data frame $X$ and $Y$ resulting in Outer join.

Question 5: What is the process to create a table in R language without using external files?

Answer:

MyTable = data.frame()
edit(MyTable)
Data Frame Questions Data Editor in R

The above code will open an Excel Spreadsheet for entering data into MyTable.

Read more about “R FAQ about Data Frame“.

https://itfeature.com

Numeric Data Type in R Language

The article is about Numeric Data Type in R Language. Decimal values are referred to as numeric data types in R, which is the default working out data type for numbers in R Language.

Numeric Data Type in R Language

Assigning a decimal value to a variable $x$ creates a variable that has a numeric data type. For example

x <- 6.2
print(x)

Since numeric data types consist of numbers, one can perform different mathematical operations such as addition, subtraction, multiplication, division, etc.

Class of Numeric Data Type

In R, the class of numeric variables is numeric. One can check the class of a numeric object ($x$) by using class() function.

class(x)
Numeric Data Type in R

Converting Character Type to Numeric Type in R

In R Language, the as.numeric() function is used to convert a vector of character values to a numeric value. Note that by default, R converts character vectors to factors.

One can confirm the data type of an object by using a function is.numeric(). For example,

is.numeric(x)

If is.numeric(x) results in an output of TRUE then it means that the data type of the variable/object $x$ is numeric. Let’s assign a whole number to a variable $y$ and then check the class of object $y$:

y <- 2
class(y)
[1] "numeric"

It means that the default data type for numbers is the numeric type in R Language. One can also use typeof() function to confirm the data type of a variable.

Creating Numeric Vectors

One can also create a variable (called a numeric vector) by using the numeric function in R. It will create a vector of zeros. For example,

z <- numeric(5)
print(z)

[1] 0 0 0 0 0

class(z)

[1] "numeric"


Other methods also exist for the creation of numeric vectors. Note that the numeric data type is different from the integer.

MCQs Data Basic Statistics Quiz

In Summary, the numeric data type in R is a fundamental data structure for numerical computations in R. Understanding its properties and when to potentially use the integer data type is essential for effective data analysis in R.

Frequently Asked Questions About R Numeric Data type in R