我想知道如何从我使用脚本读取的 txt 文件中将单个列乘以 5。我只知道如何将所有列乘以一个数字,而不是一列。这是我读取 txt 文件的脚本:
d = read.table(file="tst1.txt",header=TRUE)
我想知道如何从我使用脚本读取的 txt 文件中将单个列乘以 5。我只知道如何将所有列乘以一个数字,而不是一列。这是我读取 txt 文件的脚本:
d = read.table(file="tst1.txt",header=TRUE)
假设您的数据框d
有一个名为“数字”的列(您可以使用 来查看数据框中列的实际名称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 文档,您可以在官方页面中找到该文档。
d[,i]<-d[,i]*5
. ?Extract
有关提取部分对象的更多信息,请参阅。