如何取消列出列表列并从数据框中的取消列出列值添加新列。请在下面找到我的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")
最佳答案
由于每个列表列都有不同的结构,因此您必须以不同的方式处理它们。
使用基数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