若变量列表增加,如何在dataframe中动态添加列
如果变量列表增加,如何在dataframe中动态添加列。我的数据帧:若变量列表增加,如何在dataframe中动态添加列,r,R,如果变量列表增加,如何在dataframe中动态添加列。我的数据帧: ID Value 1 list(F="20",B="rt") 2 list(F="20",B="rt",`H'="ty") 3 list(F="20",B="rt") 4 list(F="20") 期望输出: ID Value
ID Value
1 list(F="20",B="rt")
2 list(F="20",B="rt",`H'="ty")
3 list(F="20",B="rt")
4 list(F="20")
期望输出:
ID Value F B H
1 list(F="20",B="rt") 20 rt NA
2 list(F="20",B="rt",H="ty") 20 rt ty
3 list(F="20",B="rt") 20 rt NA
4 list(F="20") 20 NA NA
structure(list(Billing = list(NULL, structure
(list(`EUcust#` = "3",`Cust#` = "5", Com = "I", `Com#` = "6", Add = "Y"), .Names
= c("EUcust#",
"Cust#", "Com", "Com#", "Add"), class
= "data.frame", row.names = 1L))), .Names
= "Billing",
row.names = 8:9, class = "data.frame")
我们可以使用
tidyverse
library(tidyverse)
df1 %>%
bind_cols(., map_df(.$Value, ~do.call(cbind.data.frame, .)))
# ID Value F B H
#1 1 20, rt 20 rt <NA>
#2 2 20, rt, ty 20 rt ty
#3 3 20, rt 20 rt <NA>
#4 4 20 20 <NA> <NA>
库(tidyverse)
df1%>%
bind_cols(,map_df(.$Value,~do.call(cbind.data.frame,))
#ID值F B H
#1120,rt 20RT
#2 20,右,右20右
#3 3 20,rt 20 rt
#4 4 20 20
数据
df1感谢您的响应,先生,根据上面的数据帧值,它工作正常。但如果行值为null,则do.call(cbind.data.frame,)中出现错误:第二个参数必须是列表。如何解决此错误,先生…@udya在这种情况下,您可以添加一个条件,即if(is.null)NA else(.)
@udya能否请您用一个小示例的dput
输出更新您的帖子,该示例显示了问题以及预期的输出,以便检查您的帖子的结构data@udya只需通过编辑structure(list)(Billing=list)(NULL,structure)(list(EUcust)
=“3”)获得dput的输出并复制/粘贴到帖子中“我”、“代码>Com”的第六方,代码>Com的第三方,代码>Com的第六方,代码>添加的第二方,代码>添加的第三方,代码>添加的第三方,Y“,.名称=c(“奥卡斯特”方,”5“,”5“,,,.名称=c(,,,,,,,,,,,,,<代码>Com,<代码>Com,<代码>Com,<代码>Com,代码>Com,代码>Com.Com.Com代码>Com,,,,,,,,,,,,,,,<代码>Com代码>Com代码>Com代码>Com代码>Com代码>Com代码>Com,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,添加
=“Y”),.Names=c(“EU cust#”、“cust#”、“Com”、“Com#”、“Add”)、.Names=“data.frame”、row.Names=1L”)、.Names=“Billing”、row.Names=8:10、class=“data.frame”)可以显示dput示例的预期输出吗
df1 <- structure(list(ID = 1:4, Value = structure(list(structure(list(
F = "20", B = "rt"), .Names = c("F", "B")), structure(list(
F = "20", B = "rt", H = "ty"), .Names = c("F", "B", "H")),
structure(list(F = "20", B = "rt"), .Names = c("F", "B")),
structure(list(F = "20"), .Names = "F")), class = "AsIs")),
.Names = c("ID",
"Value"), row.names = c(NA, -4L), class = "data.frame")