Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

r - Unlist a column of a data frame

I have the following data

library(tidyverse)
library(tibblify)

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), list(eventId = 1, subEventName = "Air duel", 
                                                                                tags = list(list(id = 703), list(id = 1801)), playerId = 103668, 
                                                                                positions = list(list(y = 81, x = 25), list(y = 83, x = 37)), 
                                                                                matchId = 2057954, eventName = "Duel", teamId = 14358, matchPeriod = "1H", 
                                                                                eventSec = 5.937411, subEventId = 10, id = 258612077), list(
                                                                                  eventId = 1, subEventName = "Air duel", tags = list(list(
                                                                                    id = 701), list(id = 1802)), playerId = 122940, positions = list(
                                                                                      list(y = 19, x = 75), list(y = 17, x = 63)), matchId = 2057954, 
                                                                                  eventName = "Duel", teamId = 16521, matchPeriod = "1H", eventSec = 6.406961, 
                                                                                  subEventId = 10, id = 258612112), list(eventId = 8, subEventName = "Simple pass", 
                                                                                                                         tags = list(list(id = 1801)), playerId = 122847, positions = list(
                                                                                                                           list(y = 17, x = 63), list(y = 15, x = 71)), matchId = 2057954, eventName = "Pass", teamId = 16521, matchPeriod = "1H", eventSec = 8.562167, 
                                                                                                                         subEventId = 85, id = 258612110)) %>% 
  tibblify()

I want the two list variables, subEventName and positions, to be either in a character format so I can later move each element in a column, or automatically create a new column for the elements of the lisst. unlist does not work.

Expected output:

> data.frame(data)
  eventId subEventName      tags playerId      positions matchId eventName teamId matchPeriod eventSec
1       8  Simple pass      1801   122671 50, 53, 50, 35 2057954      Pass  16521          1H 1.656214
2       8    High pass      1801   139393 53, 19, 35, 75 2057954      Pass  16521          1H 4.487814
3       1     Air duel 703, 1801   103668 81, 83, 25, 37 2057954      Duel  14358          1H 5.937411
4       1     Air duel 701, 1802   122940 19, 17, 75, 63 2057954      Duel  16521          1H 6.406961
5       8  Simple pass      1801   122847 17, 15, 63, 71 2057954      Pass  16521          1H 8.562167
  subEventId        id
1         85 258612104
2         83 258612106
3         10 258612077
4         10 258612112
5         85 258612110

where tags and positions are character vectors that I can later transform into separate integer columns

question from:https://stackoverflow.com/questions/65952662/unlist-a-column-of-a-data-frame

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

1 Reply

0 votes
by (71.8m points)

You can use rowwise and unlist tags and positions column.

library(dplyr)

data %>%
  rowwise() %>%
  mutate(across(c(tags, positions), ~toString(unlist(.))))

#  eventId subEventName tags      playerId positions      matchId eventName teamId matchPeriod eventSec subEventId        id
#    <dbl> <chr>        <chr>        <dbl> <chr>            <dbl> <chr>      <dbl> <chr>          <dbl>      <dbl>     <dbl>
#1       8 Simple pass  1801        122671 50, 53, 50, 35 2057954 Pass       16521 1H              1.66         85 258612104
#2       8 High pass    1801        139393 53, 19, 35, 75 2057954 Pass       16521 1H              4.49         83 258612106
#3       1 Air duel     703, 1801   103668 81, 83, 25, 37 2057954 Duel       14358 1H              5.94         10 258612077
#4       1 Air duel     701, 1802   122940 19, 17, 75, 63 2057954 Duel       16521 1H              6.41         10 258612112
#5       8 Simple pass  1801        122847 17, 15, 63, 71 2057954 Pass       16521 1H              8.56         85 258612110

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...