2017-09-11 119 views
-2

Df_01aR:操縱dataframes

Name  re1 re2 re3  parameter 
a   144 39.7 0.012  fed  
b   223 31.2  5  fed 
c   304 6.53 100  fed 
d   187 51.3 25  fed 
e   110 2.94 100  fed 
f   151 4.23 75  fed 
g   127 36.7 0.012  fed 

Df_01b

Name  re1 re2 re3  parameter 
a   142 39.3 0.042  feh  
b   221 31.0  4  feh 
c   301 6.13 90  feh 
d   185 41.3 15  feh 
e   107 2.44 940  feh 
f   143 2.23 75  feh 
g   121 31.7 0.012  feh 

Df_02

parameter c1 c2 c3 
1 fed  5  4  3 
2 feh  3  4  2 
3 fea  5  4  3 
4 few  2  4  3 

期望的結果:

c-value re-value   name 
5   142    a_fed 
4   39.3    a_fed 
3   0.042    a_fed 
5   221    b_fed       
4   31.0    b_fed 
3   4     b_fed 
5   304    c_fed 
4   6.53    c_fed 
3  100     c_fed 
.... 
3  0.012    g_fed 
3  142     a_feh 
4  39.3    a_feh 
2  0.042    a_feh 
3  221     b_feh 
4  31.0    b_feh 
2   4     b_feh 
.... 
  1. 我有Df_01a, Df_01b, Df_01c, Df_01d。這些參數分別在 第5欄:fed, feh, fea, few(參見Df_02)。

  2. 每個參數有3個值,由c1,c2c3Df_02給出。

如何獲取上面顯示的所需data.frame?

+0

這不是超我清楚你想要什麼,但我得到你的概率巧妙地想要重塑這些數據框併合並它們?如果是這樣,[本文](https://cran.r-project.org/web/packages/tidyr/vignettes/tidy-data.html)可能有你想要的。 – svenhalvorson

+1

我認爲你的預期輸出是錯誤的,'142'是一個'feh'值,而不是'fed',所有'feh'和'fed'都是倒置的 –

回答

2

代碼

library(dplyr) 
library(tidyr) 
rbind(Df_01a,Df_01b) %>% gather("re-col","re-value",c("re1","re2","re3")) %>% 
    inner_join(Df_02 %>% rename(re1=c1,re2=c2,re3=c3) %>% gather("re-col","c-value",c("re1","re2","re3"))) %>% 
    arrange(parameter,Name) %>% 
    unite(name,Name,parameter) %>% 
    select(`c-value`,`re-value`,`name`) 

結果

# c-value re-value name 
# 1  5 144.000 a_fed 
# 2  4 39.700 a_fed 
# 3  3 0.012 a_fed 
# 4  5 223.000 b_fed 
# 5  4 31.200 b_fed 
# 6  3 5.000 b_fed 
# 7  5 304.000 c_fed 
# 8  4 6.530 c_fed 
# 9  3 100.000 c_fed 
# 10  5 187.000 d_fed 
# 11  4 51.300 d_fed 
# 12  3 25.000 d_fed 
# 13  5 110.000 e_fed 
# 14  4 2.940 e_fed 
# 15  3 100.000 e_fed 
# 16  5 151.000 f_fed 
# 17  4 4.230 f_fed 
# 18  3 75.000 f_fed 
# 19  5 127.000 g_fed 
# 20  4 36.700 g_fed 
# 21  3 0.012 g_fed 
# 22  3 142.000 a_feh 
# 23  4 39.300 a_feh 
# 24  2 0.042 a_feh 
# 25  3 221.000 b_feh 
# 26  4 31.000 b_feh 
# 27  2 4.000 b_feh 
# 28  3 301.000 c_feh 
# 29  4 6.130 c_feh 
# 30  2 90.000 c_feh 
# 31  3 185.000 d_feh 
# 32  4 41.300 d_feh 
# 33  2 15.000 d_feh 
# 34  3 107.000 e_feh 
# 35  4 2.440 e_feh 
# 36  2 940.000 e_feh 
# 37  3 143.000 f_feh 
# 38  4 2.230 f_feh 
# 39  2 75.000 f_feh 
# 40  3 121.000 g_feh 
# 41  4 31.700 g_feh 
# 42  2 0.012 g_feh 

數據

Df_01a <- read.table(text="Name  re1 re2 re3  parameter 
a   144 39.7 0.012  fed  
        b   223 31.2  5  fed 
        c   304 6.53 100  fed 
        d   187 51.3 25  fed 
        e   110 2.94 100  fed 
        f   151 4.23 75  fed 
        g   127 36.7 0.012  fed",header=T,stringsAsFactors=F) 

Df_01b <- read.table(text="Name  re1 re2 re3  parameter 
         a   142 39.3 0.042  feh  
         b   221 31.0  4  feh 
         c   301 6.13 90  feh 
         d   185 41.3 15  feh 
         e   107 2.44 940  feh 
         f   143 2.23 75  feh 
         g   121 31.7 0.012  feh",header=T,stringsAsFactors=F) 

Df_02 <- read.table(text="parameter c1 c2 c3 
        1 fed  5  4  3 
        2 feh  3  4  2 
        3 fea  5  4  3 
        4 few  2  4  3",header=T,stringsAsFactors=F)