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

时间:2016-07-19 15:34:52

标签: 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")

2 个答案:

答案 0 :(得分:3)

要从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提示,指出此答案的先前版本中存在错误。

答案 1 :(得分:2)

您可以使用$ php bin/console cache:clear --env=prod --no-debug 来做您需要的事情。我希望这有用。

unlist