如何将data.frame中的单个列乘以数字
问题描述:
我想知道如何从我使用脚本读取的txt文件中将单个列乘以5。我只知道如何通过数字来叠加所有的列,但不是一个列。 这是我读txt文件脚本:如何将data.frame中的单个列乘以数字
d = read.table(file="tst1.txt",header=TRUE)
答
d[,i]<-d[,i]*5
。有关提取对象部分的更多信息,请参阅?Extract
。
答
假设您的数据帧d
有一个名为“number”的列(您可以使用str(d)
查看数据框中列的实际名称)。要乘以5列“数量”,使用:
# You are referring to the column "number" within the dataframe "d"
d$number * 5
# The last command only shoes the multiplication.
# If you want to replace the original values, use
d$number <- d$number * 5
# If you want to save the new values in a new column, use
d$numberX5 <- d$number * 5
另外,尽量参照标准的R文档,你可以在official page找到。