Dear Bert,

The easiest thing would be to merge both datasets and then multiply the corresponding columns.

both <- merge(df1, df2)
both[, 3:22] * both[, 23:42]

HTH,

Thierry

-----Oorspronkelijk bericht-----
Van: r-help-bounces@r-project.org namens Bert Jacobs
Verzonden: ma 7-6-2010 20:29
Aan: r-help@r-project.org
Onderwerp: [R] Multiply Two Dataframes
 
 

 

Hi,

I have the following two dataframes (Df1 and Df2):

Df1 has a layout like

 

ID Location  Var1 Var2 Var3 . Var20

A  Loc2          1       1        1    .       1

A  Loc1          0       1        0    .       1

B  Loc1          0       0        0    .       0       

A  Loc3          1       0        1    .       0

C  Loc3          1       0        1    .       0

B  Loc2          0       0        0    .       1       

D .

..

 

 

Df2 has a layout like

ID Var1 Var2 Var3 ..  Var20

A   0.2     0.3    0.2   ....   0.15

B   0.1     0.1     0.1  .    0.1   

C    0        0       0     .   0.50

D .

.

 

And I like to have the following result

ID Location  Var1  Var2   Var3 .    Var20

A  Loc2          0.2     0.3      0.2   .     0.15

A  Loc1          0        0.3      0      .     0.15

B  Loc1          0        0         0      .     0       

A  Loc3          0.2     0        0.2   .      0

C  Loc3          0       0        0      .       0

B  Loc2          0       0        0      .       0.1       

D .

..

 

Is this easy doable in R? I've tried to figure it out with mapply but
without result.

Thanks for helping me out.

Bert

 

 


	[[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
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.


Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.

	[[alternative HTML version deleted]]

