[R] multiplying values in data frame by corresponding value in the first column

Martin Tomko martin.tomko at geo.uzh.ch
Tue Sep 21 22:00:06 CEST 2010


I am sure there is a simple solution to this... I have a column in a 
data frame specifying a grouping (1, -1) for my observations, and need 
to mutliply each observation in all the other columns of the data frame 
by the corresponding value in the given column. I played with apply, and 
saw some suggestions for sweep, but did not manage to get it working.

My table contains NAs....

Example of table as data frame
Observation Group Param1 Param2
AA                1        3        4
BB                -1        5        6


Expected result:
Observation Group Param1 Param2
AA                1        3        4
BB                -1        -5        -6

group<-as.vector(table$Group)
I extracted the first column into a vector, and am looking for a way to 
multiply each of the columns... SO far, only manual solutions (uglyyyy) 
work: table$Param1<-group*table$Param1

Thanks many times!
Martin



More information about the R-help mailing list