R-如何根据变量增加列表在数据框中动态添加列
R-how to add dynamically column in dataframe based on list of variable increses
我需要完成以下任务;你能帮我做一下吗?
我的数据框。
U_ID Value AD CT
1 list(`Cno`="50",`cna'="jhon") ia BG
1 list(`Cno`="20",`cna'="guna") AS DB
2 list(`Cno`="30",`cna'="rt",`cf'="ty") BN FV
2 NULL VF TY
3 list(`Cno`="30") RR TT
我想要的输出是
U_ID Value Cno cna cf AD CT
1 list(`Cno`="50",`cna'="jhon") 50 jhon NULL ia BG
1 list(`Cno`="20",`cna'="guna") 20 guna NULL AS DB
2 list(`Cno`="30",`cna'="rt",`cf'="ty") 30 rt ty BN FV
2 NULL NULL NULL NULL VF TY
3 list(`Cno`="30") 30 NULL NULL RR TT
数据:
structure(list(U_ID = c(1, 1, 2, 2, 3), Value = list(structure(list(
`Cno#` = "50", cna = "jhon"), .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#` = "30"), .Names = "Cno#")),
AD = c("ia", "AS", "BN", "VF", "RR"), CT = c("BG", "DB",
"FV", "TY", "TT")), .Names = c("U_ID", "Value", "AD", "CT"
), row.names = c(NA, -5L), class = "data.frame")
您应该使用:splitstackshape::cSplit
函数。
这是 dplyr
的解决方案。
library(dplyr)
dat %>%
mutate(idx = as.character(`is.na<-`(cumsum(Value != "NULL"),
Value == "NULL"))) %>%
left_join(filter(., Value != "NULL") %>%
pull(Value) %>%
bind_rows(.id = "idx")) %>%
select(-idx)
这里,dat
是你的数据框的名字。
结果:
U_ID Value AD CT Cno# cna cf
1 1 50, jhon ia BG 50 jhon <NA>
2 1 50, guna AS DB 50 guna <NA>
3 2 30, rt, ty BN FV 30 rt ty
4 2 NULL VF TY <NA> <NA> <NA>
5 3 30 RR TT 30 <NA> <NA>
我需要完成以下任务;你能帮我做一下吗?
我的数据框。
U_ID Value AD CT
1 list(`Cno`="50",`cna'="jhon") ia BG
1 list(`Cno`="20",`cna'="guna") AS DB
2 list(`Cno`="30",`cna'="rt",`cf'="ty") BN FV
2 NULL VF TY
3 list(`Cno`="30") RR TT
我想要的输出是
U_ID Value Cno cna cf AD CT
1 list(`Cno`="50",`cna'="jhon") 50 jhon NULL ia BG
1 list(`Cno`="20",`cna'="guna") 20 guna NULL AS DB
2 list(`Cno`="30",`cna'="rt",`cf'="ty") 30 rt ty BN FV
2 NULL NULL NULL NULL VF TY
3 list(`Cno`="30") 30 NULL NULL RR TT
数据:
structure(list(U_ID = c(1, 1, 2, 2, 3), Value = list(structure(list(
`Cno#` = "50", cna = "jhon"), .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#` = "30"), .Names = "Cno#")),
AD = c("ia", "AS", "BN", "VF", "RR"), CT = c("BG", "DB",
"FV", "TY", "TT")), .Names = c("U_ID", "Value", "AD", "CT"
), row.names = c(NA, -5L), class = "data.frame")
您应该使用:splitstackshape::cSplit
函数。
这是 dplyr
的解决方案。
library(dplyr)
dat %>%
mutate(idx = as.character(`is.na<-`(cumsum(Value != "NULL"),
Value == "NULL"))) %>%
left_join(filter(., Value != "NULL") %>%
pull(Value) %>%
bind_rows(.id = "idx")) %>%
select(-idx)
这里,dat
是你的数据框的名字。
结果:
U_ID Value AD CT Cno# cna cf
1 1 50, jhon ia BG 50 jhon <NA>
2 1 50, guna AS DB 50 guna <NA>
3 2 30, rt, ty BN FV 30 rt ty
4 2 NULL VF TY <NA> <NA> <NA>
5 3 30 RR TT 30 <NA> <NA>