How to create a data frame in R?

1. From one or more vectors of the same length—by using the data.frame() function:

df <- data.frame(vector_1, vector_2)Powered By 

2. From a matrix—by using the data.frame() function:

df <- data.frame(my_matrix)Powered By 

3. From a list of vectors of the same length—by using the data.frame() function:

df <- data.frame(list_of_vectors)Powered By 

4. From other data frames:

  • To combine the data frames horizontally (only if the data frames have the same number of rows, and the records are the same and in the same order) —by using the cbind() function:
df <- cbind(df1, df2)Powered By 
  • To combine the data frames vertically (only if they have an equal number of identically named columns of the same data type and appearing in the same order) —by using the rbind() function:
df <- rbind(df1, df2)

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *