如何取消列表列并从数据框中的unlist列值中添加新列。您可以在下面找到我的df。
我的数据框架。
Status AuditResult Deship Item
Active list(DDID=c("2","First"),Dvalue=c("Bpin","67")) list(UserID=1,Add="Stet,Bpin") list(Menu=1,Bill=9,Deship =list(list(IDesc ="A",vendor=5)))
Inactive list(DDID=c("Second","8"),Dvalue=c("CA","98")) list(UserID=2,Add="Stet,Bpin") list(Menu=1,Bill=8,Deship =list(list(IDesc ="B,N,O",vendor=4)))
OnHold list(DDID=c("78","8",NA),Dvalue=c("UK","76")) list(UserID=1,Add="Stet,Bpin") list(Menu=1,Bill=7,Deship =list(list(IDesc ="L,q",vendor=2)))
预期的数据框架。
Status DDID Dvalue UserID Add Menu Bill IDesc vendor
Active "2","First" "Bpin","67" 1 "Stet,Bpin" 1 9 "A" 5
Inactive "Second","8" "CA","98" 2 "Stet,Bpin" 1 8 "B,N,O" 4
OnHold "78","8",NA "UK","76" 1 "Stet,Bpin" 1 7 "L,q" 2
如果有任何非结构化代码,请建议我。
Dput。
structure(list(Status = c("Active", "Inactive", "OnHold"), AuditResult = list(
structure(list(DDID = c(2, "First"), Dvalue = c("Bpin",
67)), .Names = c("DDID", "Dvalue"), class = "data.frame", row.names = 1:2),
structure(list(DDID = c("Second", 8), Dvalue = c("CA",
98)), .Names = c("DDID", "Dvalue"), class = "data.frame", row.names = 1:2),
structure(list(DDID = c("78","8",NA), Dvalue = c("UK",
76)), .Names = c("DDID", "Dvalue"), class = "data.frame", row.names = 1:2)),
Deship = list(structure(list(UserID = 1, Add = "Stet, Bpin"), .Names = c("UserID",
"Add"), class = "data.frame", row.names = 1L), structure(list(
UserID = 2, Add = "Stet, Bpin"), .Names = c("UserID",
"Add"), class = "data.frame", row.names = 1L), structure(list(
UserID = 1, Add = "Stet, Bpin"), .Names = c("UserID",
"Add"), class = "data.frame", row.names = 1L)), Item = list(
structure(list(Menu = 1, Bill = 9.0, Deship = list(
structure(list(IDesc = "A", vendor = 5), .Names = c("IDesc",
"vendor"), class = "data.frame", row.names = 1L))), .Names = c("Menu",
"Bill", "Deship"), class = "data.frame", row.names = 1L),
structure(list(Menu = 1, Bill = 8.0, Deship = list(
structure(list(IDesc = "B,N,O", vendor = 4), .Names = c("IDesc",
"vendor"), class = "data.frame", row.names = 1L))), .Names = c("Menu",
"Bill", "Deship"), class = "data.frame", row.names = 1L),
structure(list(Menu = 1, Bill = 7.0, Deship = list(
structure(list(IDesc = "L,q", vendor = 2), .Names = c("IDesc",
"vendor"), class = "data.frame", row.names = 1L))), .Names = c("Menu",
"Bill", "Deship"), class = "data.frame", row.names = 1L))), .Names = c("Status",
"AuditResult", "Deship", "Item"), row.names = c(NA,
3L), class = "data.frame")
答案 0 :(得分:2)
由于每个列表列都有不同的结构,因此您必须以不同的方式处理它们。
以基地R:
dfnew <- data.frame(df$Status,
do.call(rbind, lapply(df$AuditResult, function(x) sapply(x, toString))),
do.call(rbind, df$Deship),
do.call(rbind, lapply(df$Item, unlist)))
names(dfnew) <- sub('^.*\\.','',names(dfnew))
给出:
> dfnew Status DDID Dvalue UserID Add Menu Bill IDesc vendor 1 Active 2, First Bpin, 67 1 Stet, Bpin 1 9 A 5 2 Inactive Second, 8 CA, 98 2 Stet, Bpin 1 8 B,N,O 4 3 OnHold 78, 8, NA UK, 76 1 Stet, Bpin 1 7 L,q 2
答案 1 :(得分:1)
假设您的数据框名称为dt
,这是一个选项。
library(tidyverse)
dt2 <- dt %>%
select(Status, AuditResult) %>%
mutate(DDID = map(AuditResult, ~.$DDID),
Dvalue = map(AuditResult, ~.$Dvalue)) %>%
mutate(DDID = map_chr(DDID, ~toString(.)),
Dvalue = map_chr(Dvalue, ~toString(.))) %>%
select(-AuditResult)
dt3 <- dt %>%
select(Status, Deship) %>%
unnest()
dt4 <- dt %>%
select(Status, Item) %>%
unnest() %>%
unnest()
dt_final <- reduce(list(dt2, dt3, dt4), left_join, by = "Status")
dt_final
# Status DDID Dvalue UserID Add Menu Bill IDesc vendor
# 1 Active 2, First Bpin, 67 1 Stet, Bpin 1 9 A 5
# 2 Inactive Second, 8 CA, 98 2 Stet, Bpin 1 8 B,N,O 4
# 3 OnHold 78, 8, NA UK, 76 1 Stet, Bpin 1 7 L,q 2