2017-05-03 21 views
-3

我有兩個dataframes,我想他們在一個列表文件結合 我的第一個數據幀是如何轉換兩個數據幀到一個自定義列表文件

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)) 

,並在下面

df2<-structure(list(Countries = structure(c(1L, 3L, 7L, 6L, 5L, 4L, 
2L), .Label = c("Brazil", "cuba", "holand", "Italy", "Spain", 
"UK", "USA"), class = "factor"), score = c(4L, 3L, 2L, 1L, 28L, 
36L, 15L), prescore = c(1L, 25L, 9L, 24L, 65L, 27L, 31L), 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)) 
的DF2

我想將它轉化成它看起來像這樣

List of 2 
$ df1  :'data.frame': 7 obs. of 6 variables: 
    ..$ countries : chr [1:7] "India" "Bangladesh" "USA" "Germany" ... 
    ..$ score  : int [1:7] 5 3 3 36 ... 
    ..$ prescore : int [1:7] 14 2 9 30 ... 
    ..$ here  : num [1:7] 1.654 0.317 1.198 ... 
    ..$ there  : num [1:7] 0.929 0.263 0.856 ... 
    ..$ Thevalue : num [1:7] 1.309 0.165 1.150 ... 
$ df2  :'data.frame': 7 obs. of 6 variables: 
    ..$ countries : chr [1:7] "Brazil" "holand" "USA" "UK" ... 
    ..$ score  : int [1:7] 4 3 2 1 ... 
    ..$ prescore : int [1:7] 1 25 9 24 ... 
    ..$ here  : num [1:7] 1.654 0.317 1.198 ... 
    ..$ there  : num [1:7] 0.929 0.263 0.856 ... 
    ..$ Thevalue : num [1:7] 1.309 0.165 1.150 ... 

有人回答,因爲LIS列表T(DF1,DF2),但如果我這樣做,它會第一個轉換的因素

看看這裏是什麼我想

.. $國家:字符[1:7]「巴西」,「荷蘭」 「USA」「UK」...

+0

@Db如果我這樣做,那麼第一個將是因素 – nik

+0

@db謝謝你解決了這個問題,我很欣賞它很多,感謝教我 – nik

+0

@ d.b只是一件事。如何使df1和df2出現在列表中?看看輸出列表df1:'data.frame': – nik

回答

3

正如上面評論中提到的list(df1, df2)所做的那樣。對不起,我忽略了所需的輸出。用下面的

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

result <- list(df1, df2) 
str(result) 

繼續應返回所需的結果

+0

只是一件事,我接受你的答案。如何使df1和df2出現在列表中?看看輸出列表df1:'data.frame': – nik

+0

這是'str()'命令的輸出,我已經更新了回覆你的評論回覆 –

-1

考慮鑄造每個數據幀作爲列表中的列表內()函數:

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 
+0

這不是理想的結果。 – jogo

相關問題