简体   繁体   中英

Recode into new variable conditional on values in two other variables

I would like to be able to create a new variable based on specific values in two existing variables. My dataframe looks like:

structure(list(id = structure(c(1L, 2L, 3L, NA, NA, NA), .Label = c("blue", 
"red", "yellow"), class = "factor"), value = c(-4.3, -2.5, -3.6, 
NA, NA, NA)), .Names = c("id", "value"), row.names = c(NA, -6L
), class = "data.frame")

I would like to create a new column that contains only those values that pertain to blue (eg, 4.2). All other values would result in NA, like so:

structure(list(id = structure(c(1L, 2L, 3L, NA, NA, NA), .Label = c("blue", 
"red", "yellow"), class = "factor"), value = c(-4.3, -2.5, -3.6, 
NA, NA, NA), newvalue = c(-4.3, NA, NA, NA, NA, NA)), .Names = c("id", 
"value", "newvalue"), row.names = c(NA, -6L), class = "data.frame")

I tried the following:

b1 <- dat$id=="blue"
dat$newvalue <- dat$value[b1]

But that filled every cell in the new column with the same value (-4.3).

Due to presence of NA 's it becomes tricky to assign values directly using indexing. We can use replace instead where we replace any non "blue" value to NA .

dat$newvalue <- replace(dat$value, dat$id != "blue", NA)

dat
#      id value newvalue
#1   blue  -4.3     -4.3
#2    red  -2.5       NA
#3 yellow  -3.6       NA
#4   <NA>    NA       NA
#5   <NA>    NA       NA
#6   <NA>    NA       NA

The equivalent ifelse statement would be :

dat$newvalue <- ifelse(dat$id != "blue", NA, dat$value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM