我需要將新列添加到數據框中,如果多列變量列表增加(如果列有列表需要添加新列否則保持該列作爲相同)R-如何基於多個列表(列)將可變增量列動態添加到數據框中
我的數據幀,
U_ID Value AD CT value1 Citycode
1 list(`Cno`="50",'cna'="\n\rjhon\n") ia BG list(`Cno`="50") TY
1 list(`Cno`="20",`cna`="guna") AS DB list(`Cno`="\n\r20") UI
2 list(`Cno`="30",`cna`="rt",`cf`="ty") BN FV list(`Cno`="30") GH
2 NULL VF TY NULL TY
3 list(`Cno`="\n\r30") RR TT list(`Cno`="30") ST
我的願望輸出將是,
U_ID Value Cno cna cf AD CT value1 Cno1 Citycode
1 list(`Cno`="50",`cna'="\n\rjhon\n") 50 jhon NULL ia BG list(`Cno1`="50") 50 TY
1 list(`Cno`="20",`cna'="guna") 20 guna NULL AS DB list(`Cno1`="\n\r20") 20 UI
2 list(`Cno`="30",`cna'="rt",`cf'="ty") 30 rt ty BN FV list(`Cno1`="30") 30 GH
2 NULL NULL NULL NULL VF TY NULL NULL TY
3 list(`Cno`="\n\r30") 30 NULL NULL RR TT list(`Cno1`="30") 30 ST
數據,
structure(list(U_ID = c(1, 1, 2, 2, 3), Value = list(structure(list(
`Cno#` = "50", cna = "\n\rjhon\n"), .Names = c("Cno#", "cna"
)), structure(list(`Cno#` = "50", cna = "guna"), .Names = c("Cno#",
"cna")), structure(list(`Cno#` = "30", cna = "rt", cf = "ty"), .Names = c("Cno#",
"cna", "cf")), "NULL", structure(list(`Cno#` = "\n\r30"), .Names = "Cno#")),
AD = c("ia", "AS", "BN", "VF", "RR"), CT = c("BG", "DB",
"FV", "TY", "TT"), Value1 = list(structure(list(`Cno#` = "50"), .Names = "Cno#"),
structure(list(`Cno#` = "\n\r20"), .Names = "Cno#"),
structure(list(`Cno#` = "30"), .Names = "Cno#"), "NULL",
structure(list(`Cno#` = "30"), .Names = "Cno#")), Citycode = c("TY",
"UI", "GH", "RY", "ST")), .Names = c("U_ID", "Value", "AD",
"CT", "Value1", "Citycode"), row.names = c(NA, -5L), class = "data.frame")
你有沒有嘗試什麼嗎? – Sotos
您的帖子末尾的數據與開始時的數據不一樣... –
是的,我錯過了value1列表變量,col1是正確的。 –