I am trying to add a list of list into a dataframe
list1<-list(1,2,3)
list2<-NA
list3<-list(1,2,3)
list<-list(list1,list2,list3)
Mydata<-data.frame(x=NA,y=NA)
Would like to produce
x y
NA list
If you are looking to maintain a single row in y
then try this:
Mydata$y <- list(list)
If you are looking for a 3-row result try this. The second line can be omitted if it is ok if y
has AsIs
class.
Mydata2 <- data.frame(Mydata[-2], y = I(list))
class(Mydata2$y) <- NULL
or
Mydata3 <- replicate(length(list), Mydata)
Mydata3$y <- list
Please check the code below:
#Example list including other lists.
mainList = list(c(4,22,25,66),c(11,'77',2,5))
#convert list to dataframe, transpose and convert back to dataframe again
df= as.data.frame(t(as.data.frame(mainList )))
#You can set NULL to row names if required.
rownames(df)<-NULL
#Show result
df
Online compiler: https://rextester.com/OIIQ72692
Reference: R list of lists to data.frame