简体   繁体   中英

Trying to assign a number to a variable value R

I am trying to loop through the values within a categorical variable and assign a number based on whether the value is "yes" or "no"

My data is "train" and the variable is "default" which looks as follow:

default = c("no", "yes", "no"....)

I want to create a separate vector which contains thee number 10 for any value of "yes" and the number 1 for nay value "no."

I tried:

wgts = c()
for (y in 1:nrow(train)) {
  ifelse(train$default[y] == "yes", wgts = append(wgts[y], 10), wgts = append(wgts[y], 1))
  return(wgts)
}

But the resulting vector is turning out to be NULL. How can I fix this?

There is no need for a for loop here, just use:

ifelse(default == "yes", 10,1)

This assumes you only have yes or no in your vector. A short sample:

default <- c("yes","no")

ifelse(default == "yes", 10,1)

If you need some more speed, you can simply subset the vector:

default[default=="yes"] <- 10
default[default=="no"]<-1
default <- as.numeric(default)

This way you overwrite your default vector.

An option with case_when

library(dplyr)
case_when(default == 'yes' ~ 10, TRUE ~ 1)

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