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
1.1k views
in Technique[技术] by (71.8m points)

r - How to add new column from list column value

How to unlist list column and add new column from unlist column value in data frame.Can you please find my df below.

My data Frame.

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)))

Expected data frame.

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

please suggest me if any unstructured code.

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")
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

As each of the list columns has a different structure, you'll have to deal with them in different ways.

With base 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))

which gives:

> 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

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

...