# R FAQs about Data Frame

The post contains R FAQs about the Data frame. It is in the form of questions and answers.

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.

Question 3: How will you drop variables using indices in a 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

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 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 dataframe.
• $Y$ represents the second dataframe.
• $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?

MyTable = data.frame()
edit(MyTable)

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