[英]Subtracting an entire column by a value
I am new to R and need a little help.我是 R 新手,需要一些帮助。 I have a data set with 13 columns.
我有一个包含 13 列的数据集。 One column has height measurements and is appropriately titled "Height".
一列具有高度测量值,并适当地命名为“高度”。 I want to subtract 2 from each value in that column.
我想从该列中的每个值中减去 2。 What function would I use to do so?
我将使用什么功能来做到这一点?
As in the comments,正如评论中所说,
yourdata$Height <- yourdata$Height - 2
will work to subtract two from the dataset "yourdata" and the column "Height", and then it replaces "<-" that column "yourdata$Height".将从数据集“yourdata”和“Height”列中减去两个,然后替换“<-”那列“yourdata$Height”。
If you want to keep the original column as is, and add a new column, then you need to change the name of your new 'object'.如果要保持原始列不变,并添加新列,则需要更改新“对象”的名称。 Such as:
如:
yourdata$HeightMinus2 <- yourdata$Height - 2
In addition to comments which work, a "tidy" way to do this is:除了有效的评论之外,一个“整洁”的方法是:
library(dplyr)
new_df <- initial_df %>% mutate(Height = Height - 2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.