r - 如何将数据框中的列表元素转换为数据框

标签 r list dataframe

我有一个包含一些列表项的数据框,在该数据框中我想将所有列表项转换为数据框。示例数据框df。我想要没有任何列表项的数据框。

structure(list(Study = structure(c(1L, 3L, 2L), .Label = c("new", 
"y", "z"), class = "factor"), IQC = structure(list(result.1 = 4, 
    result.2 = 20, result.3 = 2.67), .Names = c("result.1", "result.2", 
"result.3")), EQC = structure(list(result.1 = "1.12*", result.2 = "0.9*", 
    result.3 = 3.1), .Names = c("result.1", "result.2", "result.3"
)), CQCg = structure(list(result.1 = 307.65, result.2 = 307.65, 
    result.3 = 16.16), .Names = c("result.1", "result.2", "result.3"
)), CQCp = structure(list(result.1 = 22.27, result.2 = 20.93, 
    result.3 = 9.59), .Names = c("result.1", "result.2", "result.3"
)), AQCg = structure(list(result.1 = 10.8, result.2 = 8.99, result.3 = 8.37), .Names = c("result.1", 
"result.2", "result.3")), AQCp = structure(list(result.1 = 3.81, 
    result.2 = "1.07*", result.3 = "0.2*"), .Names = c("result.1", 
"result.2", "result.3")), Rank = c(1.42, 1.92, 2.67)), .Names = c("Study", 
"IQC", "EQC", "CQCg", "CQCp", "AQCg", "AQCp", "Rank"), row.names = c(NA, 
3L), class = "data.frame")

最佳答案

要从 data.frame 中的任何列中删除 list 类,只需使用

df[] <- unlist(df)

转换后,data.frame 中显示的条目保持不变:

#> df
#  Study  IQC   EQC   CQCg  CQCp AQCg  AQCp Rank
#1     1    4 1.12* 307.65 22.27 10.8  3.81 1.42
#2     3   20  0.9* 307.65 20.93 8.99 1.07* 1.92
#3     2 2.67   3.1  16.16  9.59 8.37  0.2* 2.67

但是可以验证列表列是否已转换为字符列:

#> sapply(df,class)
#  Study         IQC         EQC        CQCg        CQCp        AQCg        AQCp        Rank 
#"character" "character" "character" "character" "character" "character" "character" "character" 

PS:向@docendodiscimus 致敬,感谢他指出了此答案之前版本中的错误。

关于r - 如何将数据框中的列表元素转换为数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38462903/

相关文章:

python - Pandas - 比较多个数据帧的值,并保留多数值

r - 来自 "sampling"的 strata() 返回错误 : arguments imply differing number of rows

c# - 列出操作复杂度

java - 数组列表。按产品类型按文章访问名称

c# - 对列表中对象的属性使用 Math.NET 统计函数的最佳方法

python - 从 python 数据框的列构造二分图

python - 使用前 10 个值和下一个值之间的平均值替换 pandas 数据框中的特定值

r - 在 vector/data.frame 列上应用返回 data.frame/tibble 的函数并绑定(bind)结果

r - dplyr 使用 lubridate::hhmm 格式和 minute() 进行过滤

r - 使用条件子集数据框并将每个子集保存为新数据框