m@ithiii_shiv@ m@iii@g oii y@hoo@com
2022-Apr-08 12:17 UTC
[R] Multiplying each row of data.frame by a row in another data.frame
Dear R forum weights <- data.frame(id = c("ABC", "DEF",? "ONS"), value = c(1, 2, 5)) val_df <- data.frame(ABC = c(10, 20, 10, 10, 10), DEF = c(1, 2, 10, 2, 5), ONS = c(100, 100, 200, 100, 100))> weights? ?id? ? ? ? ? ?value1 ABC? ? ? ?12 DEF? ? ? ?23 ONS? ? ? ?5> val_df? ABC DEF ONS1? 10? ?1? ? ?1002? 20? ?2? ? ?1003? 10? 10? ? 2004? 10? ?2? ? 1005? 10? ?5? ? 100 I wish to multilpy each row of data.frame val_df by value column of data.frame weights. ? ABC? ? DEF? ? ONS1? 10*1? ?1*2? ?100*52? 20*1? ?2*2? ?100*53? 10*1? ?10*2? 200*54? 10*1? ?2*2? ?100*55? 10*1? ?5*2? ?100*5 ie I wish to have output as OUTPUT ? ABC? ? DEF? ? ONS1? 10? ? 2? ? ? ? ? 5002? 20? ? 4? ? ? ? ?5003? 10? ? 20? ? ? 10004? 10? ? 4? ? ? ? 5005? 10? ? 10? ? ?500 O have tried sweep function, even matrix multiplcation etc, but nothing seems to be working. Please guide Regards Maithili [[alternative HTML version deleted]]
Eric Berger
2022-Apr-09 08:58 UTC
[R] Multiplying each row of data.frame by a row in another data.frame
This looks like homework, which is not permitted on this list. But giving you the benefit of the doubt, here are two ways to do it> as.matrix(val_df) * matrix(rep(weights$value,5),ncol=3,byrow = TRUE)ABC DEF ONS [1,] 10 2 500 [2,] 20 4 500 [3,] 10 20 1000 [4,] 10 4 500 [5,] 10 10 500> t(apply(as.matrix(val_df),MAR=1,function(v) v * weights$value))HTH, Eric On Sat, Apr 9, 2022 at 7:57 AM maithili_shiva--- via R-help <r-help at r-project.org> wrote:> > Dear R forum > weights <- data.frame(id = c("ABC", "DEF", "ONS"), value = c(1, 2, 5)) > val_df <- data.frame(ABC = c(10, 20, 10, 10, 10), DEF = c(1, 2, 10, 2, 5), ONS = c(100, 100, 200, 100, 100)) > > > weights > id value1 ABC 12 DEF 23 ONS 5 > > > val_df > ABC DEF ONS1 10 1 1002 20 2 1003 10 10 2004 10 2 1005 10 5 100 > I wish to multilpy each row of data.frame val_df by value column of data.frame weights. > ABC DEF ONS1 10*1 1*2 100*52 20*1 2*2 100*53 10*1 10*2 200*54 10*1 2*2 100*55 10*1 5*2 100*5 > ie I wish to have output as > OUTPUT > ABC DEF ONS1 10 2 5002 20 4 5003 10 20 10004 10 4 5005 10 10 500 > > O have tried sweep function, even matrix multiplcation etc, but nothing seems to be working. > Please guide > Regards > Maithili > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Kelly Thompson
2022-Apr-09 16:49 UTC
[R] Multiplying each row of data.frame by a row in another data.frame
Does this produce the desired results? Also, even if this produces your desired results, I am not sure it is the "best" way to do this. There might be better ways. data.frame( t ( t(val_df) * (weights$value) ) On Fri, Apr 8, 2022 at 9:57 PM maithili_shiva--- via R-help <r-help at r-project.org> wrote:> > Dear R forum > weights <- data.frame(id = c("ABC", "DEF", "ONS"), value = c(1, 2, 5)) > val_df <- data.frame(ABC = c(10, 20, 10, 10, 10), DEF = c(1, 2, 10, 2, 5), ONS = c(100, 100, 200, 100, 100)) > > > weights > id value1 ABC 12 DEF 23 ONS 5 > > > val_df > ABC DEF ONS1 10 1 1002 20 2 1003 10 10 2004 10 2 1005 10 5 100 > I wish to multilpy each row of data.frame val_df by value column of data.frame weights. > ABC DEF ONS1 10*1 1*2 100*52 20*1 2*2 100*53 10*1 10*2 200*54 10*1 2*2 100*55 10*1 5*2 100*5 > ie I wish to have output as > OUTPUT > ABC DEF ONS1 10 2 5002 20 4 5003 10 20 10004 10 4 5005 10 10 500 > > O have tried sweep function, even matrix multiplcation etc, but nothing seems to be working. > Please guide > Regards > Maithili > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.