简体   繁体   中英

How can I separate a list of lists in a dataframe?

I am analysing a dataframe with traintrips. The data is formatted as such:

 tripnumber stop       
<int> <list>     
1 <list [34]>
2 <list [34]>
3 <list [33]>
4 <list [20]>
5 <list [17]>
6 <list [17]>

Each tripnumber is connected to a certain amount of stops, trip 1 has for example 34 stops.

An important note is that the stop lists are not lists of just stations, but these are formatted as another lists with station + information (let's call these stationlists), structured like this:

list(Station = "ams", Arival_time = "0135", Departure_time = "0138", Index = "1")

I want to have the list of stationlists unlisted with in the first column after the tripnumber the first stationlist, in the second column the second stationlist, etc. such that it should look like below:

 tripnumber stop1 stop2 stop3 stop4 stop5 .... 
<int> <list>     
1 <list [4]> <list [4]> <list [4]> <list [4]> <list [4]> ....
2 <list [4]> <list [4]> <list [4]> <list [4]> <list [4]> ....
3 <list [4]> <list [4]> <list [4]> <list [4]> <list [4]> ....
4 <list [4]> <list [4]> <list [4]> <list [4]> <list [4]> ....
5 <list [4]> <list [4]> <list [4]> <list [4]> <list [4]> ....
6 <list [4]> <list [4]> <list [4]> <list [4]> <list [4]> ....

I tried to format this with the purrr library. However I am not too familiar with this package and the difficulty is that I cannot get this working without losing the tripnumber structure, or the "stationlist" structure.

Any tips how to solve this?

Edits:

  • The following dput(head(traintrips) can be copy pasted to R as testfile: .txt file
  • If there are more stop columns than actual stops, the cell should remain empty (" ")

Got it working by unnesting and additionaly reshape the result by using the following code:

DFnew <- unnest(traintrips, traintrips$stop) 
DFnew$time <- with(DFnew, ave(tripnumber, tripnumber, FUN = seq_along)) # add time column
names(DFnew)[2] <- paste("stop") # to remove the dollar sign from the colname of the unnested data
DFnew <- spread(DFnew, time, stop)

With as result:

> dim(DFnew)
[1]  6 35

> head(DFnew[,1:6])
# A tibble: 6 x 6
  tripnumber `1`        `2`        `3`        `4`        `5`       
       <int> <list>     <list>     <list>     <list>     <list>    
1          1 <list [4]> <list [4]> <list [4]> <list [4]> <list [4]>
2          2 <list [4]> <list [4]> <list [4]> <list [4]> <list [4]>
3          3 <list [4]> <list [4]> <list [4]> <list [4]> <list [4]>
4          4 <list [4]> <list [4]> <list [4]> <list [4]> <list [4]>
5          5 <list [4]> <list [4]> <list [4]> <list [4]> <list [4]>
6          6 <list [4]> <list [4]> <list [4]> <list [4]> <list [4]>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM