-5

I am new to R and need a little help. I have a data set with 13 columns. One column has height measurements and is appropriately titled "Height". I want to subtract 2 from each value in that column. What function would I use to do so?

  • 4
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. You can probably just do `mydata$Height <- mydata$Height-2` – MrFlick Jan 27 '19 at 23:52
  • yourdata$Height <- yourdata$Height - 2 – Chase Jan 27 '19 at 23:54

2 Answers2

2

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".

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
Dylan_Gomes
  • 2,066
  • 14
  • 29
1

In addition to comments which work, a "tidy" way to do this is:

library(dplyr)
new_df <- initial_df %>% mutate(Height = Height - 2)