繁体   English   中英

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

[英]How to multiply a single column in a data.frame by a number

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

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

让我们假设您的数据框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文档,您可以在官方页面中找到它。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM