I want to know what's the difference between data.frame and as.data.frame
Look at this simple example I created below. Here we have a vector consisting of 1 to 6. I am able to take that vector and coerce it into a dataframe with
as.data.frame which then gives me a
data.frame with only one column in it. If you run the code as well you'd be able to see.
data.frame is when we setup data to take on the form of a
data.frame. So therefore it already has the states and properties of a
as.data.frame is just a convenient function to do this conversion for us on the fly so we can
cbind as example these new bits of data to an existing
library(tidyverse) vector <- c(1,2,3,4,5,6) str(vector) #> num [1:6] 1 2 3 4 5 6 vector <- vector %>% as.data.frame() str(vector) #> 'data.frame': 6 obs. of 1 variable: #> $ .: num 1 2 3 4 5 6 df <- data.frame(a = c(1,2,3,4,5,6), b = c(2,3,4,5,6,7)) str(df) #> 'data.frame': 6 obs. of 2 variables: #> $ a: num 1 2 3 4 5 6 #> $ b: num 2 3 4 5 6 7
Created on 2020-10-26 by the reprex package (v0.3.0)
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.