I know it is a basic quaestion but couldnt find any solution to it.
I want to multiply all columns of a dataframe by single column.
df1<-data.frame(F1=c(1,5,8,9),F2=c(1,5,8,9),F3=c(1,5,8,9))
> df1
F1 F2 F3
1 1 1 1
2 5 5 5
3 8 8 8
4 9 9 9
C<-data.frame(C=c(2,1,2,0.5))
> C
C
1 2.0
2 1.0
3 2.0
4 0.5
I wanna multiply each column of df1
by C.
I have tried:
df2<-df1[,c(1:3)]*C[,1]
df2<-df1*C
df2< df1%*%C
and
for(i in 2:length(names(b))) {
+ df2 <- C[, 1] * b[, i]
+ }
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…