R:操纵数据帧

时间:2017-09-11 13:57:45

标签: r

Df_01a

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给出。

  3. 如何获得上面显示的所需data.frame?

1 个答案:

答案 0 :(得分:2)

<强>码

(?=.*[[:alpha:]])[[:alnum:]]{1}[[:alnum:] -]*[[:alnum:]]{1}

<强>结果

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