Head and toes Head and toes - 3 months ago 9
R Question

Convert list with numeric and string variables to dataframe in R

I am wondering how I can convert a list with both numeric and string variables to a dataframe:

For example:

aa<-c("a","b","b","b","d")
bb<-c("Yes","No","No","Yes","Yes")
cc<-c(1,2,4,4,3)

x<-list(aa=aa,bb=bb,cc=cc)


How can I convert x to a dataframe, such that when I call x I get:

aa bb cc
1 a Yes 1
2 b No 2
3 b No 4
4 b Yes 4
5 d Yes 3


Thanks!

Answer

You can do x<-as.data.frame(x).

Updated based on @Frank's good below, if you want to avoid converting characters to factors do

x<-as.data.frame(x, StringsAsFactors = FALSE).

Comments