As.data.frame aligns a nested list into one line instead of creating a line for each record

I have a nested list that looks like this:

mylist <- vector("list", 2) mylist[[1]]$name <- "The Tucson IOT Meetup Group" mylist[[1]]$state <- "AZ" mylist[[2]]$name <- "#SFMySQL Meetup" mylist[[2]]$state <- "CA" mylist [[1]] [[1]]$name [1] "The Tucson IOT Meetup Group" [[1]]$state [1] "AZ" [[2]] [[2]]$name [1] "#SFMySQL Meetup" [[2]]$state [1] "CA" 

I would like to turn this into a data frame with columns "name" and "state" and two rows, one for each record. But when I try to use as.data.frame for this, I return one row of data with separate columns for each of the record variables, for example:

 myframe <- as.data.frame(mylist) myframe name state name.1 state.1 1 The Tucson IOT Meetup Group AZ #SFMySQL Meetup CA 

I'm not sure what is going on. What is the right way to do this?

+5
source share
2 answers
 do.call(rbind.data.frame, mylist) ## name state ## 2 The Tucson IOT Meetup Group AZ ## 21 #SFMySQL Meetup CA 

The do.call function somewhat resembles the (l / s) apply functions and allows functions to accumulate the results of successive calls. The Reduce function sometimes does the same thing:

  Reduce(rbind.data.frame, mylist) ## name state ##2 The Tucson IOT Meetup Group AZ ##21 #SFMySQL Meetup CA 

You can even rbind "work" with Reduce :

 Reduce(rbind, mylist) ## name state ##init "The Tucson IOT Meetup Group" "AZ" ## "#SFMySQL Meetup" "CA" 

It was initially thought to be the best result. (I prefer results that convey symbolic values ​​rather than factors.) However, both Reduce approaches deliver rather strange structures when viewed with str ().

+4
source

Or try the data.table rbindlist function (very efficient for large data sets)

 library(data.table) rbindlist(mylist) # name state # 1: The Tucson IOT Meetup Group AZ # 2: #SFMySQL Meetup CA 
+3
source

Source: https://habr.com/ru/post/1203091/


All Articles