如何将data.frame中的单个列乘以数字

时间:2013-02-27 03:05:16

标签: r

我想知道如何从我使用脚本读取的txt文件中将单个列多出5个。我只知道如何通过数字而不是单个列来填充所有列。 这是我阅读txt文件的脚本:

d = read.table(file="tst1.txt",header=TRUE)

2 个答案:

答案 0 :(得分:8)

假设您的数据框d有一个名为“number”的列(您可以使用str(d)查看数据框中列的实际名称)。要将列“number”乘以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中找到它。

答案 1 :(得分:2)

d[,i]<-d[,i]*5。有关提取对象部分的更多信息,请参阅?Extract

相关问题