Home > Software engineering >  R generating the difference of two column in a data frame
R generating the difference of two column in a data frame

Time:12-06

I want to create a new variable that is the difference of two other variables.

In particular, I tried to the following code:

> DF <- data.frame(A = rnorm(10), B = rnorm(10), C = A - B)

But, the code does not work. So, how can I do that without generating new object?

p.s. I do not want to do the following one:

> a <- rnorm(10)
> b <- rnorm(10)
> DF <- data.frame(A = a, B = b, C = a - b)

CodePudding user response:

Here a solution with dplyr

library(dplyr)

DF <- data.frame(A = rnorm(10), B = rnorm(10))

mutate(DF,C = A - B)
  • Related