Add a column to dataframe in R, based on greater than or less than condition in previous columns

12,711

You can use function ifelse :

x <- data.frame(medv=rnorm(10, mean=30), rm=rnorm(10, mean=7))
x$CATMEDV <- ifelse(x$medv>30.0 & x$rm>7, 1, 0)
x
       medv       rm CATMEDV
1  30.59482 8.139964       1
2  29.64808 6.565482       0
3  30.99625 5.875143       0
4  31.46609 7.848523       1
5  29.65703 7.875010       0
6  29.47165 7.023216       0
7  28.26122 8.046844       0
8  31.62395 6.007356       0
9  32.10948 7.423388       1
10 29.53155 6.098616       0
Share:
12,711

Related videos on Youtube

vagabond
Author by

vagabond

R user. learning SAS &amp; Python

Updated on September 15, 2022

Comments

  • vagabond
    vagabond over 1 year

    A very basic question. I have a data frame with 14 variables and 576 observations.

    >  head(Boston)
         crim zn indus chas   nox    rm  age    dis rad tax ptratio  black lstat medv
    1 0.00632 18  2.31    0 0.538 6.575 65.2 4.0900   1 296    15.3 396.90  4.98 24.0
    2 0.02731  0  7.07    0 0.469 6.421 78.9 4.9671   2 242    17.8 396.90  9.14 21.6
    3 0.02729  0  7.07    0 0.469 7.185 61.1 4.9671   2 242    17.8 392.83  4.03 34.7
    4 0.03237  0  2.18    0 0.458 6.998 45.8 6.0622   3 222    18.7 394.63  2.94 33.4
    5 0.06905  0  2.18    0 0.458 7.147 54.2 6.0622   3 222    18.7 396.90  5.33 36.2
    6 0.02985  0  2.18    0 0.458 6.430 58.7 6.0622   3 222    18.7 394.12  5.21 28.7
    
    1. I want to create a new column CATMEDV populated with a value of 1 or 0 for each record. If medv > 30.0, CATMEDV=1, IF medv < 30, CATMEDV=0

    2. If I want to add multiple conditions, say medv>30.0 and rm>7 then CATMEDV=1, else 0. How can I do this?

    Also, everytime I paste a data frame from my R Console to a question, it gets jumbled and some moderator re-formats it. How do I paste data frames in question windows without muddling the format from R?

  • vagabond
    vagabond about 10 years
    thanks! I was trying to use transform unnecessarily !