Reputation: 513
I need to multiply each column in a data.frame
by the values in first column (classfactor).
This is my data.frame
sample classfactor 01.BA.V 01.BA.VG 01.BO.VG 01.PR.O 01.TO.VG 02.BA.O 02.BA.V
AB 0.730 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0 0.00000000
AC 0.730 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0 0.00000000
AB 0.002 0.000000000 0.000000000 0.000749929 0.000000000 0.000000000 0 0.00000000
CC 0.730 0.081599145 0.093453018 0.031247022 0.015987076 0.036212483 0 0.02537884
BB 0.730 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0 0.00000000
AA 0.730 0.001533075 0.000108666 0.000000000 0.000364526 0.000241417 0 0.00006340
It has 146 rows and 155 columns.
I can't figure out how to do. Any suggestions?
Upvotes: 2
Views: 4507
Reputation: 99371
If I'm not mistaken, you can do
df[-(1:2)] <- df[["classfactor"]] * df[-(1:2)]
where df
is your data frame and the first column is sample
(classfactor
is actually the second column).
Upvotes: 8
Reputation: 2283
Well, you could start with a loop.
for(i in 2:length(names(data.frame))) {
data.frame[,i] <- data.frame[, 1] * data.frame[, i]
}
On a dataset as small as yours, this should be reasonably quick.
Upvotes: 2