向 data.frame 添加一个新列,其因子取决于另一个 data.frame 的条件

Adding a new column to data.frame with a factor depending conditions from another data.frame

我有 2 个不同的 data.frames。我想在组data.frame(LEVEL 和SPECIES)给定的条件下将grouping$.group 列添加到物候data.frame。我已经尝试使用 by= 的 merge() 函数,但它一直给我 "Error in fix.by(by.y, y) : 'by' must specify a uniquely valid column"。抱歉,这似乎是一件非常简单的事情。我是初学者..

    > head(phenology1)
  YEAR GRADIENT               SPECIES ELEVATION SITE TREE_ID CN b_E   b_W   b_M   d_E   d_W d_X   c_E c_W t_max     r_max r_delta_t LEVEL
1 2019        1 Pseudotsuga menziesii       395 B1_D   B1_D1 59 119 135.5 143.0 139.0 148.5 165 258.0 284   154 0.7908536 0.4244604 lower
2 2019        1 Pseudotsuga menziesii       395 B1_D   B1_D2 69 106 127.0 142.0 177.0 173.0 194 283.0 300   156 0.9807529 0.3898305 lower
3 2019        1 Pseudotsuga menziesii       395 B1_D   B1_D3 65  97 125.0 154.5 169.0 174.0 202 266.0 299   167        NA 0.3846154 lower
4 2019        1           Picea abies       405 B1_F   B1_F1 68 162 171.5 182.0 106.5 127.5 137 268.5 299   190        NA 0.6384977 lower
5 2019        1           Picea abies       405 B1_F   B1_F2 78 139 165.5 176.5 152.0 140.5 167 291.0 306   181 0.9410427 0.5131579 lower
6 2019        1           Picea abies       405 B1_F   B1_F3 34 147 177.5 188.0 100.0  97.5 128 247.0 275   187 0.5039245 0.3400000 lower

> grouping
 LEVEL SPECIES               emmean   SE df lower.CL upper.CL .group
 lower Pseudotsuga menziesii    107 8.19 12     89.5      125  1    
 upper Pseudotsuga menziesii    122 8.19 12    103.8      140  12   
 lower Abies alba               128 8.19 12    110.2      146  12   
 upper Abies alba               144 8.19 12    126.7      162  12   
 upper Picea abies              147 8.19 12    129.2      165   2   
 lower Picea abies              149 8.19 12    131.5      167   2   

您可以使用 dplyr 包中的 left_join()(仅使用列 LEVEL、SPECIES 和来自分组的 .group 加入 phenology1):

library(dplyr)

phenology1 %>% 
  left_join(grouping %>% select(LEVEL, SPECIES, .group))

这会自动选择两个数据框中相同的列名进行连接。如果你想明确地设置这些,你可以添加 by = c("LEVEL" = "LEVEL", "SPECIES" = "SPECIES").

使用匹配函数的 Base R:

phenology1$.group <- grouping$.group[match(grouping$SPECIES, phenology1$SPECIES) & match(grouping$LEVEL, phenology1$LEVEL)]