Home > Software design >  If column A equals criteria return value of column B in column C
If column A equals criteria return value of column B in column C

Time:07-01

Using the R inbuilt dataset

mtcars

I want to make a column called "want".

mtcars$want<-NA

When column "carb" is equal to 1 (Column A), input value of column "qsec" (Column B) in column "want" (Column C).

If carb is not equal to 1 do nothing.

The first 5 rows of the new dataset should look like this:

                    mpg  cyl disp  hp  drat wt    qsec   vs am  gear carb want
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4   NA
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4   NA
Datsun              22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1   18.61
Hornet Drive        21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1   19.44
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2   NA

CodePudding user response:

This should do the job:

mtcars$want <- ifelse(mtcars$carb == 1, mtcars$qsec, NA)
head(mtcars, 5)

                   mpg cyl disp  hp drat    wt  qsec vs am gear carb  want
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4    NA
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4    NA
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1 18.61
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 19.44
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2    NA

If you only want to achieve it in the print out you could try the following (in the data.frame itself this will still be shown as NA):

mtcars$want <- ifelse(mtcars$carb == 1, mtcars$qsec, "")
head(mtcars, 5)

                   mpg cyl disp  hp drat    wt  qsec vs am gear carb  want
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4      
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4      
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1 18.61
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 19.44
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2      

CodePudding user response:

If it is helpful, I am of the impression that a loop over the columns should work. One can modify the loop or add further conditionals as appropriate to fill in the other values of the column.

#written in R version 4.2.1
data(mtcars)
mtcars$want = 0
for(i in 1:dim(mtcars)[1]){
if(mtcars$carb[i] == 1){
mtcars$want[i] = mtcars$qsec[i]
}}

Result:

head(mtcars)
#                   mpg cyl disp  hp drat    wt  qsec vs am gear carb  want
#Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4  0.00
#Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4  0.00
#Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1 18.61
#Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 19.44
#Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2  0.00
#Valiant 

CodePudding user response:

What you can do is first set a value to your new column "want" for example 2. You can use ifelse to do your criteria and return "want" if do nothing like this:

mtcars$want <- 2

library(dplyr)
mtcars %>%
  mutate(want = ifelse(carb == 1, qsec, want)) %>%
  head(5)
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb  want
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4  2.00
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4  2.00
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1 18.61
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 19.44
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2  2.00

Created on 2022-06-30 by the reprex package (v2.0.1)

  • Related