如何在R中使用查找表和公式创建计算列?

如何在R中使用查找表和公式创建计算列?,r,calculated-columns,R,Calculated Columns,我有一个如下所示的数据帧: V1 V2 V3 3 4 3 2 4 3 4 4 3 4 4 4 1 4 2 4 2 4 4 4 1 4 4 2 3 4 1 4 4 4 4 4 2 4 4 2 2 1 2 3 2 3 3 4 3 3 4

我有一个如下所示的数据帧:

   V1  V2   V3
   3    4   3
   2    4   3
   4    4   3
   4    4   4
   1    4   2
   4    2   4
   4    4   1
   4    4   2
   3    4   1
   4    4   4
   4    4   2
   4    4   2
   2    1   2
   3    2   3
   3    4   3
   3    4   2
   4    4   2
   4    4   4
   2    3   3
   3    4   1
 V_id   coeff  weight 
  V1   0.82     4.77   
  V2   0.75     4.77   
  V3   0.67     4.77
我还有这样一个查找表:

   V1  V2   V3
   3    4   3
   2    4   3
   4    4   3
   4    4   4
   1    4   2
   4    2   4
   4    4   1
   4    4   2
   3    4   1
   4    4   4
   4    4   2
   4    4   2
   2    1   2
   3    2   3
   3    4   3
   3    4   2
   4    4   2
   4    4   4
   2    3   3
   3    4   1
 V_id   coeff  weight 
  V1   0.82     4.77   
  V2   0.75     4.77   
  V3   0.67     4.77
我想在查找表中使用这些值,使用

(V1*coeff+V2*coeff+V3*coeff)/weight

最后的数据帧应该是这样的

   V1   V2  V3  new_column
   3    4   3   1.566037736
   2    4   3   1.394129979
   4    4   3   1.737945493
   4    4   4   1.878406709
   1    4   2   1.081761006
   4    2   4   1.5639413
   4    4   1   1.457023061
   4    4   2   1.597484277
   3    4   1   1.285115304
   4    4   4   1.878406709
   4    4   2   1.597484277
   4    4   2   1.597484277
   2    1   2   0.78197065
   3    2   3   1.251572327
   3    4   3   1.566037736
   3    4   2   1.42557652
   4    4   2   1.597484277
   4    4   4   1.878406709
   2    3   3   1.236897275
   3    4   1   1.285115304

我必须对1125列的数据帧执行此操作

我们可以将第一个数据转换为长格式,然后按行号分组以获得计算列

library(dplyr)
library(tidyr)
df1 %>% 
   mutate(rn = row_number()) %>%
   pivot_longer(cols = -rn, names_to = "V_id") %>% 
   left_join(df2)  %>% 
   group_by(rn) %>% 
   summarise(new_column = sum(coeff *value)/weight[1]) %>% 
   select(new_column) %>% 
   bind_cols(df1, .)
# A tibble: 20 x 4
#      V1    V2    V3 new_column
#   <int> <int> <int>      <dbl>
# 1     3     4     3      1.57 
# 2     2     4     3      1.39 
# 3     4     4     3      1.74 
# 4     4     4     4      1.88 
# 5     1     4     2      1.08 
# 6     4     2     4      1.56 
# 7     4     4     1      1.46 
# 8     4     4     2      1.60 
# 9     3     4     1      1.29 
#10     4     4     4      1.88 
#11     4     4     2      1.60 
#12     4     4     2      1.60 
#13     2     1     2      0.782
#14     3     2     3      1.25 
#15     3     4     3      1.57 
#16     3     4     2      1.43 
#17     4     4     2      1.60 
#18     4     4     4      1.88 
#19     2     3     3      1.24 
#20     3     4     1      1.29 
数据
df1编辑更新问题的更新答案(数据框有1125列):



备选方案:

df1$new_column <- as.matrix(df1) %*% c(0.82, 0.75, 0.67) / 4.77

df1$new\u专栏我编辑了我的问题。我的数据框有1125列。所以这将是非常低效的。谢谢你,谢谢。我已经更新了我的答案。如果这不起作用,或者如果您还有其他编辑,请告诉我。:)
library(dplyr)

df %>%
  mutate(new_column = (V1*0.82 + V2*0.75 + V3*0.67) / 4.77)
   V1 V2 V3 new_column
1   3  4  3  1.5660377
2   2  4  3  1.3941300
3   4  4  3  1.7379455
4   4  4  4  1.8784067
5   1  4  2  1.0817610
6   4  2  4  1.5639413
...
df1$new_column <- as.matrix(df1) %*% c(0.82, 0.75, 0.67) / 4.77