简体   繁体   English

数据框的每个值乘以ID搜索的另一数据框的一行

[英]Multiply each value of a dataframe by a row of another dataframe searched by id

I'm new with R and I have tried a lot to solve this problem, if anyone could help me I'd be very grateful! 我是R的新手,我已经尝试了很多解决此问题的方法,如果有人可以帮助我,我将非常感激! This is my problem: 这是我的问题:

I have two data frame (df1 and df2) and what I need is to multiply each value of df1 by a row of df2 searched by id. 我有两个数据帧(df1和df2),我需要将df1的每个值乘以ID搜索的df2行。 This is an example of what I'm looking for: 这是我正在寻找的示例:

df1<-data.frame(ID=c(1,2,3), x1=c(6,3,2), x2=c(2,3,1), x3=c(4,10,7))
df1
df2<-data.frame(ID=c(1,2,3), y1=c(0.01,0.02,0.05), y2=c(0.2,0.03,0.11), y3=c(0.3,0.09,0.07))
df2

#Example of what I need
df1xdf2<- data.frame(ID=c(1,2,3), r1=c(0.06,0.06,0.1), r2=c(1.2,0.09,0.22), r3=c(1.8,0.27,0.14),
                     r4=c(0.02,0.06,0.05),r5=c(0.4,0.09,0.11),r6=c(0.6,0.27,0.07),r7=c(0.04,0.2,0.35),r8=c(0.8,0.3,0.77),r9=c(1.2,0.9,0.49))

df1xdf2

I've tried with loops by row and column but I only get a 1x1 multiplication. 我已经尝试过按行和列进行循环,但是我只能得到1x1的乘法。

My dataframes have same number of rows, columns and factor names. 我的数据框具有相同数量的行,列和因子名称。 My real life dataframes are much larger, both rows and columns. 我的现实生活数据帧无论行还是列都更大。 Does anyone know how to solve it? 有人知道如何解决吗?

You could use lapply to multiply every column of df1 with complete df2 . 您可以使用lapplydf1每一列与完整的df2相乘。 We can cbind the dataframes together and rename the columns 我们可以将数据cbind在一起并重命名列

output <- do.call(cbind, lapply(df1[-1], `*`, df2[-1]))
cbind(df1[1], setNames(output, paste0("r", seq_along(output))))

#  ID   r1   r2   r3   r4   r5   r6   r7   r8   r9
#1  1 0.06 1.20 1.80 0.02 0.40 0.60 0.04 0.80 1.20
#2  2 0.06 0.09 0.27 0.06 0.09 0.27 0.20 0.30 0.90
#3  3 0.10 0.22 0.14 0.05 0.11 0.07 0.35 0.77 0.49

You could use the dplyr package 您可以使用dplyr软件包

#Example with dplyr

require(dplyr)
# First we use merge() to join both DF
result <- merge(df1, df2, by = "ID") %>% 
  mutate(r1 = x1*y1,
         r2 = x1*y2,
         r3 = etc.)

within mutate() you can specify your new column formulas and names 在mutate()中,您可以指定新的列公式和名称

An option with map map选项

library(tidyverse)
bind_cols(df1[1], map_dfc(df1[-1], `*`, df2[-1]))

Or in base R by replicating the columns and multiplying 或在base R通过复制列并相乘

out <- cbind(df1[1], df1[-1][rep(seq_along(df1[-1]), each = 3)] *
         df2[-1][rep(seq_along(df2[-1]), 3)])
names(out)[-1] <- paste0("r", seq_along(out[-1]))

out
#  ID   r1   r2   r3   r4   r5   r6   r7   r8   r9
#1  1 0.06 1.20 1.80 0.02 0.40 0.60 0.04 0.80 1.20
#2  2 0.06 0.09 0.27 0.06 0.09 0.27 0.20 0.30 0.90
#3  3 0.10 0.22 0.14 0.05 0.11 0.07 0.35 0.77 0.49

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM