1

I have what I consist a tricky situation. I used a nested lapply statement and I am trying to extract the results into a dataframe. The method I used to extract the result is below. The first element in the list is the year of the data, with 1 being 2014, 2 2015 etc. I used other approach which produced the same outcome

runs_results_1 <- as.data.frame(Reduce(rbind, runs_year[[1]]))

The real problem is that when I run the following code

sapply(runs_results_1, mode)

It returns the following for the dataframe. With the columns being lists it is annoying to use the data.

statistic     p.value        runs          mu         var      method   
data.name 
 "list"      "list"      "list"      "list"      "list"      "list"      
 "list" 
alternative     players 
 "list"      "list" 

Is there a way to convert each of the columns from a list into a numeric or character column? I realize i am not providing a workable example and from other possibles it seems this is a technical answer as to how R structure things. Any help would be appreciated.

Matthew Oldham
  • 187
  • 1
  • 13
  • Is the best approach `cc <- data.frame(matrix(unlist(runs_results_1), nrow = 681, byrow = T))` once I know the number of columns? – Matthew Oldham Apr 11 '17 at 19:11
  • It would be easier to help if you provided a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Maybe show how you created this object in the first place; there may be a better way. – MrFlick Apr 11 '17 at 19:31
  • Sorry @MrFlick but it would have been very messy to do over this forum. I have a workable solution now but it probably is not the most efficient way – Matthew Oldham Apr 12 '17 at 00:02

1 Answers1

0

If your column lists are single element atomic lists, then you can use the "unlist" command, e.g.:

df <- sapply(df, unlist)
thc
  • 9,527
  • 1
  • 24
  • 39
  • Thanks. This did work and does not rely on knowing the number of rows in advance. It just needs a small adjust due to the collection not being an atomic vector – Matthew Oldham Apr 12 '17 at 00:01