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 (" ")