[英]An efficient way to create a new dataframe based on values from 2 separate dataframes in R
I would like help finding an efficient way to create a new df, based on the values of 2 separate dfs: the first contains peoples scores on a set of items and the second contains weights for each item.我想帮助找到一种基于 2 个单独 dfs 的值来创建新 df 的有效方法:第一个包含人们对一组项目的分数,第二个包含每个项目的权重。 I want to create a new df that multiplies the values in df1 with their respective weights in df2 for scoring purposes.
我想创建一个新的 df ,将 df1 中的值乘以它们在 df2 中的各自权重以用于评分目的。 A small example of what I mean.
我的意思的一个小例子。 Data frames 1 and 2:
数据帧 1 和 2:
it1<-c(0,1,2,0,1,2)
it2<-c(1,1,2,1,2,1)
it3<-c(0,2,1,1,1,0)
df1<-cbind(it1,it2,it3)
it<-c("it1","it2","it3")
val<-c(2,4,6)
df2<-cbind.data.frame(it,val)
I would like my new df to multiply the values from df1 by the appropriate weights identified in df2 (so item1 is multiplied by 2, item 2 is multiplied by 4, and item 3 is multiplied by 6).我希望我的新 df 将 df1 中的值乘以 df2 中标识的适当权重(因此 item1 乘以 2,item 2 乘以 4,item 3 乘以 6)。 I would like the new df to look like this:
我希望新的 df 看起来像这样:
it1 it2 it3
0 4 0
2 4 12
4 8 6
0 4 6
2 8 6
4 4 0
my actual data has more items and weights than this example我的实际数据比这个例子有更多的项目和权重
If the columns are in the same order如果列的顺序相同
df1 * df2$val[col(df1)]
Or as @markus mentioned或者正如@markus 提到的
df1[,df2$it] * as.list(df2$val)
There are multiple ways to tackle this.有多种方法可以解决这个问题。 One roundabout option is to convert it to long format and then do a join and reshape it back to 'wide' format
一种迂回选择是将其转换为长格式,然后进行连接并将其重新整形为“宽”格式
library(dplyr)
library(tidyr)
df1 %>%
mutate(rn = row_number()) %>%
pivot_longer(cols = -rn) %>%
left_join(df2, c('name' = 'it')) %>%
mutate(value = val* value) %>%
select(-val) %>%
pivot_wider(names_from = name, values_from = value) %>%
select(-rn)
NOTE: Here we assume the datasets are all data.frame
注意:这里我们假设数据集都是
data.frame
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.