Creating a new column to a data frame using a formula from another variable

后端 未结 3 1669
醉梦人生
醉梦人生 2020-12-29 03:27

I want to create a new column to a data frame using a formula from another variable.
Example:
I have a data set \"aa\" is;

x    y 
2    3 
4             


        
3条回答
  •  被撕碎了的回忆
    2020-12-29 04:13

    You should probably read some basic tutorials on R other than An Introduction to R as despite what is written there the $ notation is more sensible and easier to understand than attach/detach. Try this in the meantime.

    aa <- data.frame(x = c(2, 4, 6), y = c(3, 5, 7))
    

    Which gives:

    > aa
      x y
    1 2 3
    2 4 5
    3 6 7
    

    Then enter:

    aa$z <- (aa$x + aa$y) - 2
    

    Which gives:

    > aa
      x y  z
    1 2 3  3
    2 4 5  7
    3 6 7 11
    

提交回复
热议问题