R 如何将两个数据帧转换为一个自定义列表文件

R 如何将两个数据帧转换为一个自定义列表文件,r,R,我有两个数据帧,我想将它们合并到一个列表文件中 我的第一个数据帧是 df1 <-structure(list(Countries = structure(c(4L, 2L, 7L, 3L, 6L, 5L, 1L), .Label = c("Argentina", "Bangladesh", "Germany", "India", "Italy", "Spain", "USA"), class = "factor"), score = c(5L, 3L, 3L, 36L, 27L,

我有两个数据帧,我想将它们合并到一个列表文件中 我的第一个数据帧是

 df1 <-structure(list(Countries = structure(c(4L, 2L, 7L, 3L, 6L, 5L, 
1L), .Label = c("Argentina", "Bangladesh", "Germany", "India", 
"Italy", "Spain", "USA"), class = "factor"), score = c(5L, 3L, 
3L, 36L, 27L, 38L, 42L), prescore = c(14L, 2L, 9L, 30L, 23L, 
28L, 30L), Here = c(1.654426641, 0.317580534, 1.198132543, 1.159607938, 
0.502336763, 0.258959426, 3.590781184), There = c(0.929521231, 
0.263531469, 0.856353826, 1.492219621, 0.446449268, 0.295339446, 
2.365211352), Thevalue = c(1.309899094, 0.165288467, 1.150147005, 
1.622165884, 0.528522213, 0.634062442, 1.733429701)), .Names = c("Countries", 
"score", "prescore", "Here", "There", "Thevalue"), class = "data.frame", row.names = c(NA, 
-7L))
有些人回答为列表(df1,df2),但如果我这样做,它会将第一个转换为因子

看看这里,我想要什么


..$countries:chr[1:7]“巴西”、“荷兰”、“美国”和“英国”..

正如上面评论中所提到的
列表(df1、df2)
完成了这项工作。抱歉,我忽略了所需的输出。继续执行以下步骤

df1$Countries <- as.character(df1$Countries)
df2$Countries <- as.character(df2$Countries)

df1$Countries考虑将每个数据帧强制转换为list()函数中的列表:

df_list <- list(as.list(df1),as.list(df2))
df_list

[[1]]
[[1]]$Countries
[1] India      Bangladesh USA        Germany    Spain      Italy      Argentina 
Levels: Argentina Bangladesh Germany India Italy Spain USA

[[1]]$score
[1]  5  3  3 36 27 38 42

[[1]]$prescore
[1] 14  2  9 30 23 28 30

[[1]]$Here
[1] 1.65443 0.31758 1.19813 1.15961 0.50234 0.25896 3.59078

[[1]]$There
[1] 0.92952 0.26353 0.85635 1.49222 0.44645 0.29534 2.36521

[[1]]$Thevalue
[1] 1.30990 0.16529 1.15015 1.62217 0.52852 0.63406 1.73343


[[2]]
[[2]]$Countries
[1] Brazil holand USA    UK     Spain  Italy  cuba  
Levels: Brazil cuba holand Italy Spain UK USA

[[2]]$score
[1]  4  3  2  1 28 36 15

[[2]]$prescore
[1]  1 25  9 24 65 27 31

[[2]]$Here
[1] 1.65443 0.31758 1.19813 1.15961 0.50234 0.25896 3.59078

[[2]]$There
[1] 0.92952 0.26353 0.85635 1.49222 0.44645 0.29534 2.36521

[[2]]$Thevalue
[1] 1.30990 0.16529 1.15015 1.62217 0.52852 0.63406 1.73343

df_list@d.b如果我这样做,那么第一个factor@d.b谢谢你解决了这个问题,我非常感激,谢谢你的教导me@d.b只有一件事。如何使df1和df2出现在列表中?查看输出列表df1:'data.frame':@d.b很抱歉打扰您,但是如何将此添加到列表(df1,df2)中,这不是期望的结果。只需一件事,我接受您的答案。如何使df1和df2出现在列表中?查看输出列表df1:'data.frame':这是
str()
命令的输出,我已经更新了答案以响应您的评论
df1$Countries <- as.character(df1$Countries)
df2$Countries <- as.character(df2$Countries)
result <- list(df1, df2)
str(result)
df_list <- list(as.list(df1),as.list(df2))
df_list

[[1]]
[[1]]$Countries
[1] India      Bangladesh USA        Germany    Spain      Italy      Argentina 
Levels: Argentina Bangladesh Germany India Italy Spain USA

[[1]]$score
[1]  5  3  3 36 27 38 42

[[1]]$prescore
[1] 14  2  9 30 23 28 30

[[1]]$Here
[1] 1.65443 0.31758 1.19813 1.15961 0.50234 0.25896 3.59078

[[1]]$There
[1] 0.92952 0.26353 0.85635 1.49222 0.44645 0.29534 2.36521

[[1]]$Thevalue
[1] 1.30990 0.16529 1.15015 1.62217 0.52852 0.63406 1.73343


[[2]]
[[2]]$Countries
[1] Brazil holand USA    UK     Spain  Italy  cuba  
Levels: Brazil cuba holand Italy Spain UK USA

[[2]]$score
[1]  4  3  2  1 28 36 15

[[2]]$prescore
[1]  1 25  9 24 65 27 31

[[2]]$Here
[1] 1.65443 0.31758 1.19813 1.15961 0.50234 0.25896 3.59078

[[2]]$There
[1] 0.92952 0.26353 0.85635 1.49222 0.44645 0.29534 2.36521

[[2]]$Thevalue
[1] 1.30990 0.16529 1.15015 1.62217 0.52852 0.63406 1.73343