简体   繁体   English

基于 R 中 2 个单独数据帧的值创建新 dataframe 的有效方法

[英]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.

相关问题 从 R 数据框中的多列创建新变量的有效方法 - efficient way to create a new variable from multiple columns in R dataframe 基于嵌套 if else 条件创建新列并比较 R 中列表值的有效方法 - Efficient way to create new column based on nested if else conditions and comparing values from lists in R 基于另一个 dataframe 中的行创建多个新数据帧,并在 r 中使用 for 循环 - Create multiple new dataframes based on rows in another dataframe with a for loop in r 尝试根据来自另一个的NA值使用参考数据框创建新的数据框 - Attempting to create new dataframes using a reference dataframe based on NA values from another 根据 R 中不同字段中的值添加新字段的有效方法 - Efficient way to add a new field based on values in a different field in R 从 R 中的两个单独的数据帧形成一个数据帧 - Forming a Dataframe from two separate Dataframes in R R中是否有办法将数据帧过滤并将其拆分为新的数据帧? - Is there a way in R to filter and split a dataframe into new dataframes? R在数据框上循环以创建新的数据框 - R loop over dataframe to create new dataframes 基于 R 中的列表创建新的数据框 - Create new dataframes based on a list in R 基于分组 dataframe 使用 ZE28396D3D40DZAF17 中的 dplyr 创建具有多个汇总列的 dataframe 的有效方法 - Efficient way to create a dataframe with multiple summary columns based on a grouped dataframe using dplyr in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM