muthu m
2017-Nov-21 15:13 UTC
[R] R-How to unlist data frame multiple structured list column value and new column
Hi, How to unlist list column value and add column into data frame. Data frame ID ContractDe PassengersDe TrainnerDe 1 list(ConID=c("Zx","78yu"),ConRes = c("98","Tut")) list(PassID =1,PassIt="Lits,uy") list(Trnid=1,Trncont =5,EmpAddInfo=list(list(CohID ="pi",InVoice=77))) 2 list(ConID=c("Half","Yut","Weq"),ConRes =c("ref","Cr")) list(PassID =c("pfil","Q"),Name ="Tic",PassIt="S5,Y1 list(Trnid=7,Trncont =3,EmpAddInfo=list(list(CohID =c("AB","NI","OL"),InVoice = c("4","Y")))) 3 list(ConID=c("Ui","pric"),ConRes = c("Num","Patch")) list(PassID =1,PassIt ="St,Bp") list(Trnid=c("U", "l"),Trncont=c("10","78"),EmpAddInfo=list(list(CohID =c("AB","NI","OL"),InVoice =c("4","Y")))) 4 list(ConID=c("2","7","IO"),ConRes = c("Res","Qty"),ConVal =c("Wno", "ip")) list(PassID =1,Name ="RT",Name1 ="RR",PassIt="st7,st9") list(Trnid=c("1", "3"),Trncont=c("yt","re"),EmpAddInfo=list(list(CohID =c("Ly","qp"),InVoice =c("2","P")))) Expected data frame. ID ConID ConRes ConVal PassID PassIt Name Name1 Trnid Trncont CohID InVoice 1 Zx,78yu 98,Tut NA 1 Lits,uy NA NA 1 5 pi 77 2 Half,Yut,Weq ref,Cr NA pfil,Q S5,Y1 Tic NA 7 3 AB,NI,OL 4,Y 3 Ui,pric Num,Patch NA 1 St,Bp NA NA U,l 10,78 AB,NI,OL 4,Y 4 2,7,IO Res,Qty Wno,ip 1 st7,st9 RT st7,st9 1,3 yt,re Ly,qp 2,P dput structure(list(ID = c("1", "2", "3","4"), ContractDe = list( structure(list(ConID = c("Zx", "78yu"), ConRes = c("98", "Tut")), .Names = c("ConID", "ConRes"), class = "data.frame", row.names = 1:2), structure(list(ConID = c("Half", "Yut","Weq"), ConRes = c("ref", "Cr")), .Names = c("ConID", "ConRes"), class = "data.frame", row.names = 1:2), structure(list(ConID = c("Ui", "pric"), ConRes = c("Num", "Patch")), .Names = c("ConID", "ConRes"), class = "data.frame", row.names = 1:2), structure(list(ConID = c("2", "7","IO"), ConRes = c("Res", "Qty"),ConVal=c("Wno","ip")), .Names = c("ConID", "ConRes","ConVal"), class = "data.frame", row.names = 1:2)), PassengersDe = list(structure(list(PassID = 1, PassIt = "Lits, uy"), .Names = c("PassID", "PassIt"), class = "data.frame", row.names = 1L), structure(list( PassID = c("pfil","Q"), Name = "Tic", PassIt = "S5, Y1"), .Names = c("PassID", "Name", "PassIt"), class = "data.frame", row.names = 1L), structure(list(PassID = 1, PassIt = "St, Bp"), .Names = c("PassID", "PassIt"), class = "data.frame", row.names = 1L), structure(list(PassID = 1, Name = "RT", Name1 = "RR", PassIt = "st7, st9"), .Names = c("PassID", "Name", "Name1", "PassIt"), class = "data.frame", row.names = 1L)), TrainnerDe = list(structure(list(Trnid = 1, Trncont = 5, EmpAddInfo = list( structure(list(CohID = "pi", InVoice = 77), .Names = c("CohID", "InVoice"), class = "data.frame", row.names = 1L))), .Names = c("Trnid", "Trncont", "EmpAddInfo"), class = "data.frame", row.names = 1L), structure(list(Trnid =7,Trncont = 3, EmpAddInfo = list(structure(list( CohID = c("AB", "NI", "OL"), InVoice = c("4", "Y")), .Names = c("CohID", "InVoice"), class = "data.frame", row.names = 1L))), .Names = c("Trnid", "Trncont", "EmpAddInfo"), class = "data.frame", row.names = 1L), structure(list(Trnid =c("U","l"),Trncont =c("10","78"), EmpAddInfo = list(structure(list( CohID = c("AB", "NI", "OL"), InVoice = c("4", "Y")), .Names = c("CohID", "InVoice"), class = "data.frame", row.names = 1L))), .Names = c("Trnid", "Trncont", "EmpAddInfo"), class = "data.frame", row.names = 1L), structure(list(Trnid = c("1","3"), Trncont = c("yt","re"), EmpAddInfo = list(structure(list( CohID = c("Ly","qp"), InVoice = c("2","P")), .Names = c("CohID", "InVoice" ), class = "data.frame", row.names = 1L))), .Names = c("Trnid", "Trncont", "EmpAddInfo"), class = "data.frame", row.names = 1L))), .Names = c("ID", "ContractDe", "PassengersDe", "TrainnerDe"), row.names = c(NA, 4L), class = "data.frame") i was stuck on this change part of my project, please help me to out this. Thanks. Sent from Outlook<http://aka.ms/weboutlook> [[alternative HTML version deleted]]
David Winsemius
2017-Nov-22 02:53 UTC
[R] R-How to unlist data frame multiple structured list column value and new column
> On Nov 21, 2017, at 7:13 AM, muthu m <muthu.psg.swift at gmail.com> wrote: > > Hi, > > How to unlist list column value and add column into data frame.After looking at this and playing with the output of dput on the first 4 rows in this sample of what I suspect is a much larger data object my suspicion is that this cam from a JSON file or a SQL file and got inappropriately rearranged into a dataframe. It probably should have been processed differently. My advice would be to go back a couple of steps to the original data input process and see if you can deliver a less mangled version of your data. -- David.> > Data frame > > ID ContractDe PassengersDe TrainnerDe > > 1 list(ConID=c("Zx","78yu"),ConRes = c("98","Tut")) list(PassID =1,PassIt="Lits,uy") list(Trnid=1,Trncont =5,EmpAddInfo=list(list(CohID ="pi",InVoice=77))) > > 2 list(ConID=c("Half","Yut","Weq"),ConRes =c("ref","Cr")) list(PassID =c("pfil","Q"),Name ="Tic",PassIt="S5,Y1 list(Trnid=7,Trncont =3,EmpAddInfo=list(list(CohID =c("AB","NI","OL"),InVoice = c("4","Y")))) > > 3 list(ConID=c("Ui","pric"),ConRes = c("Num","Patch")) list(PassID =1,PassIt ="St,Bp") list(Trnid=c("U", "l"),Trncont=c("10","78"),EmpAddInfo=list(list(CohID =c("AB","NI","OL"),InVoice =c("4","Y")))) > > 4 list(ConID=c("2","7","IO"),ConRes = c("Res","Qty"),ConVal =c("Wno", "ip")) list(PassID =1,Name ="RT",Name1 ="RR",PassIt="st7,st9") list(Trnid=c("1", "3"),Trncont=c("yt","re"),EmpAddInfo=list(list(CohID =c("Ly","qp"),InVoice =c("2","P")))) > > > > > > Expected data frame. > > ID ConID ConRes ConVal PassID PassIt Name Name1 Trnid Trncont CohID InVoice > > 1 Zx,78yu 98,Tut NA 1 Lits,uy NA NA 1 5 pi 77 > > 2 Half,Yut,Weq ref,Cr NA pfil,Q S5,Y1 Tic NA 7 3 AB,NI,OL 4,Y > > 3 Ui,pric Num,Patch NA 1 St,Bp NA NA U,l 10,78 AB,NI,OL 4,Y > > 4 2,7,IO Res,Qty Wno,ip 1 st7,st9 RT st7,st9 1,3 yt,re Ly,qp 2,P > > > > dput > > > structure(list(ID = c("1", "2", "3","4"), ContractDe = list( > structure(list(ConID = c("Zx", "78yu"), ConRes = c("98", > "Tut")), .Names = c("ConID", "ConRes"), class = "data.frame", row.names = 1:2), > structure(list(ConID = c("Half", "Yut","Weq"), ConRes = c("ref", > "Cr")), .Names = c("ConID", "ConRes"), class = "data.frame", row.names = 1:2), > structure(list(ConID = c("Ui", "pric"), ConRes = c("Num", > "Patch")), .Names = c("ConID", "ConRes"), class = "data.frame", row.names = 1:2), > structure(list(ConID = c("2", "7","IO"), ConRes = c("Res", > "Qty"),ConVal=c("Wno","ip")), .Names = c("ConID", "ConRes","ConVal"), class = "data.frame", row.names = 1:2)), > PassengersDe = list(structure(list(PassID = 1, PassIt = "Lits, uy"), .Names = c("PassID", > "PassIt"), class = "data.frame", row.names = 1L), structure(list( > PassID = c("pfil","Q"), Name = "Tic", PassIt = "S5, Y1"), .Names = c("PassID", > "Name", "PassIt"), class = "data.frame", row.names = 1L), > structure(list(PassID = 1, PassIt = "St, Bp"), .Names = c("PassID", > "PassIt"), class = "data.frame", row.names = 1L), > structure(list(PassID = 1, Name = "RT", Name1 = "RR", PassIt = "st7, st9"), .Names = c("PassID", > "Name", "Name1", "PassIt"), class = "data.frame", row.names = 1L)), > TrainnerDe = list(structure(list(Trnid = 1, Trncont = 5, EmpAddInfo = list( > structure(list(CohID = "pi", InVoice = 77), .Names = c("CohID", > "InVoice"), class = "data.frame", row.names = 1L))), .Names = c("Trnid", > "Trncont", "EmpAddInfo"), class = "data.frame", row.names = 1L), > structure(list(Trnid =7,Trncont = 3, EmpAddInfo = list(structure(list( > CohID = c("AB", "NI", "OL"), InVoice = c("4", "Y")), .Names = c("CohID", > "InVoice"), class = "data.frame", row.names = 1L))), .Names = c("Trnid", > "Trncont", "EmpAddInfo"), class = "data.frame", row.names = 1L), > structure(list(Trnid =c("U","l"),Trncont =c("10","78"), EmpAddInfo = list(structure(list( > CohID = c("AB", "NI", "OL"), InVoice = c("4", "Y")), .Names = c("CohID", > "InVoice"), class = "data.frame", row.names = 1L))), .Names = c("Trnid", > "Trncont", "EmpAddInfo"), class = "data.frame", row.names = 1L), > structure(list(Trnid = c("1","3"), Trncont = c("yt","re"), EmpAddInfo = list(structure(list( > CohID = c("Ly","qp"), InVoice = c("2","P")), .Names = c("CohID", "InVoice" > ), class = "data.frame", row.names = 1L))), .Names = c("Trnid", > "Trncont", "EmpAddInfo"), class = "data.frame", row.names = 1L))), .Names = c("ID", > "ContractDe", "PassengersDe", "TrainnerDe"), row.names = c(NA, 4L), class = "data.frame") > > > i was stuck on this change part of my project, please help me to out this. Thanks. > > > > Sent from Outlook<http://aka.ms/weboutlook> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius Alameda, CA, USA 'Any technology distinguishable from magic is insufficiently advanced.' -Gehm's Corollary to Clarke's Third Law