Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

r - multiply all the columns in a data.frame by the first

I need to multiply each column in a data.frameby 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...