【问题标题】:Convert lists within a data frame into vectors将数据框中的列表转换为向量
【发布时间】:2017-02-23 02:22:23
【问题描述】:

我有以下数据框:

Offer ID    Item ID  
162840  56A340942123, 902G569800234  
162841  96A3403718213, 872M569814535, M235481980234, 890TG56248191  
162842  H7901230913592

Offer ID 是一个字符向量,Item ID 是一个列表。在这里,项目 ID 有 3 个元素 - 每行一个。

是否可以将项目 ID 转换为列表中的向量,以便我可以分别引用同一行中的每个项目 ID?

我对 R 比较陌生,我尝试过 unlist、do.call 和 paste,但它们都弄乱了我的数据框的尺寸。

注意:我不想将它们拆分为子列表。

ETA:dput的输出-

structure(list(Col1 = c(162840L, 162841L, 162842L, 162843L, 162845L, 
162847L, 162849L), Col2 = structure(list(`1` = c("137089", "668552", 
"346129"), `4` = c("442054", "479934", "58316"), `7` = c("149298", 
"533977", "598069"), `10` = c("898134", "614982", "581007", "570515"
), `14` = c("93015", "252103", "639482", "226594", "64429"), 
    `19` = c("328971", "604454", "603078"), `22` = "93774"), .Names = c("1", 
"4", "7", "10", "14", "19", "22"))), .Names = c("Col1", "Col2"
), row.names = c(NA, -7L), class = "data.frame")

预期输出:

Offer ID    Item ID1    Item ID2    Item ID3    Item ID4  
168240      137089      668552      346129  
162841      442054      479934      58316  
162842      149298      533977      598069  
162843      898134      614982      581007      570515  

【问题讨论】:

  • 使用cSplit(df1, "item ID", ",", "long")
  • @akrun 你指的是splitstackshape::cSplit吗?你真的应该限定非标准函数的来源。
  • 您的预期输出究竟是什么? (顺便说一句:您的 dputed 数据与您的第一个 data.frame 不匹配。这里还有其他事情吗?)
  • @r2evans 是的,忘记了。
  • @r2evans - 我已经编辑了原始问题以包含预期的输出并且对数据感到抱歉,我为项目 ID 创建了两个带有随机数的单独文件(我还没有实际值),忘记保存我在原始问题中使用的那个。

标签: r list vector


【解决方案1】:
# get length of the largest vector
max_cols <- max( unlist( lapply(df1$Col2, length ) ) )

# fill with NA to make it equal length
a1 <- lapply( df1$Col2, function( x ) c( x, rep( NA, ( max_cols - length( x ) ) ) ) ) 

# combine column 1 with transformed data
df1 <- do.call( 'cbind', list( df1$Col1, do.call( 'rbind', a1 ) ) )

# apply column names
colnames( df1 ) <- paste( "Col", 1: (max_cols+1), sep = '' )

# convert to numeric data type
df1 <- data.frame( apply( df1, 2, as.numeric ) )

df1
#     Col1   Col2   Col3   Col4   Col5  Col6
# 1 162840 137089 668552 346129     NA    NA
# 2 162841 442054 479934  58316     NA    NA
# 3 162842 149298 533977 598069     NA    NA
# 4 162843 898134 614982 581007 570515    NA
# 5 162845  93015 252103 639482 226594 64429
# 6 162847 328971 604454 603078     NA    NA
# 7 162849  93774     NA     NA     NA    NA

数据:

df1 <- structure(list(Col1 = c(162840L, 162841L, 162842L, 162843L, 162845L, 162847L, 162849L), 
                      Col2 = structure(list(`1` = c("137089", "668552", "346129"),
                                            `4` = c("442054", "479934", "58316"),
                                            `7` = c("149298", "533977", "598069"),
                                            `10` = c("898134", "614982", "581007", "570515"),
                                            `14` = c("93015", "252103", "639482", "226594", "64429"), 
                                            `19` = c("328971", "604454", "603078"), 
                                            `22` = "93774"), 
                                       .Names = c("1", "4", "7", "10", "14", "19", "22"))),
                 .Names = c("Col1", "Col2"), 
                 row.names = c(NA, -7L), 
                 class = "data.frame")

【讨论】:

    猜你喜欢
    • 2017-09-25
    • 1970-01-01
    • 2021-01-18
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-20
    相关资源
    最近更新 更多