Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

r - All data in a row. Move observations under each other

I have the following data:

data <- list(list(eventId = 8, subEventName = "Simple pass", tags = list(
  list(id = 1801)), playerId = 122671, positions = list(list(
    y = 50, x = 50), list(y = 53, x = 35)), matchId = 2057954, 
  eventName = "Pass", teamId = 16521, matchPeriod = "1H", eventSec = 1.656214, 
  subEventId = 85, id = 258612104), list(eventId = 8, subEventName = "High pass", 
                                         tags = list(list(id = 1801)), playerId = 139393, positions = list(
                                           list(y = 53, x = 35), list(y = 19, x = 75)), matchId = 2057954, 
                                         eventName = "Pass", teamId = 16521, matchPeriod = "1H", eventSec = 4.487814, 
                                         subEventId = 83, id = 258612106))

I want to create a data frame out of this list. I use unlist(data), which creates a row with repeated variables.

> unlist(data)
      eventId  subEventName       tags.id      playerId   positions.y   positions.x   positions.y 
          "8" "Simple pass"        "1801"      "122671"          "50"          "50"          "53" 
  positions.x       matchId     eventName        teamId   matchPeriod      eventSec    subEventId 
         "35"     "2057954"        "Pass"       "16521"          "1H"    "1.656214"          "85" 
           id       eventId  subEventName       tags.id      playerId   positions.y   positions.x 
  "258612104"           "8"   "High pass"        "1801"      "139393"          "53"          "35" 
  positions.y   positions.x       matchId     eventName        teamId   matchPeriod      eventSec 
         "19"          "75"     "2057954"        "Pass"       "16521"          "1H"    "4.487814" 
   subEventId            id 
         "83"   "258612106" 

Each observation starts with the eventId variable. So, basically I have to split the data into dataframes starting with eventId, and then moving those dataframes one under the other. I.e. having two observations in this case. Do you have any idea? thanks in advance

question from:https://stackoverflow.com/questions/65946359/all-data-in-a-row-move-observations-under-each-other

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try tibblify--

library(tibblify)

tibblify(data)
## A tibble: 2 x 12
#  eventId subEventName               tags playerId          positions matchId eventName teamId matchPeriod eventSec subEventId        id
#    <dbl> <chr>        <list<tbl_df[,1]>>    <dbl> <list<tbl_df[,2]>>   <dbl> <chr>      <dbl> <chr>          <dbl>      <dbl>     <dbl>
#1       8 Simple pass             [1 x 1]   122671            [2 x 2] 2057954 Pass       16521 1H              1.66         85 258612104
#2       8 High pass               [1 x 1]   139393            [2 x 2] 2057954 Pass       16521 1H              4.49         83 258612106

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...