简体   繁体   中英

Recoding variables in R using function

I'm trying to recode a variable whose scale is the following: 0, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. I want to work with a scale that just increases naturally from 0 to 11. I am using the following (clunky) code:

for (i in 1:22){
  if (data2[i,"mus_post_borg_di"] == 0) {
    data2[i,"mus_post_borg_di_rescale"] <- 0
  }
  else if (data2[i,"mus_post_borg_di"] == 11) {
    data2[i,"mus_post_borg_di_rescale"] <- 1
  }
  else if (data2[i,"mus_post_borg_di"] == 1) {
    data2[i,"mus_post_borg_di_rescale"] <- 2
  }
  else if (data2[i,"mus_post_borg_di"] == 2) {
    data2[i,"mus_post_borg_di_rescale"] <- 3
  }
  else if (data2[i,"mus_post_borg_di"] == 3) {
    data2[i,"mus_post_borg_di_rescale"] <- 4
  }
  else if (data2[i,"mus_post_borg_di"] == 4) {
    data2[i,"mus_post_borg_di_rescale"] <- 5
  }
  else if (data2[i,"mus_post_borg_di"] == 5) {
    data2[i,"mus_post_borg_di_rescale"] <- 6
  }
  else if (data2[i,"mus_post_borg_di"] == 6) {
    data2[i,"mus_post_borg_di_rescale"] <- 7
  }
  else if (data2[i,"mus_post_borg_di"] == 7) {
    data2[i,"mus_post_borg_di_rescale"] <- 8
  }
  else if (data2[i,"mus_post_borg_di"] == 8) {
    data2[i,"mus_post_borg_di_rescale"] <- 9
  }
  else if (data2[i,"mus_post_borg_di"] == 9) {
    data2[i,"mus_post_borg_di_rescale"] <- 10
  }
  else if (data2[i,"mus_post_borg_di"] == 10) {
    data2[i,"mus_post_borg_di_rescale"] <- 11
  }
}

Running this recodes things like I want. However, since I am working with other variables using the same scale, I decided to write a function that would avoid unnecessary copy/pasting:

borg_rescale_fct <- function(x, y){
 for (i in 1:22){
  if (data2[i,x] == 0) {
    data2[i,y] <- 0
  }
  else if (data2[i,x] == 11) {
    data2[i,y] <- 1
  }
  else if (data2[i,x] == 1) {
    data2[i,y] <- 2
  }
  else if (data2[i,x] == 2) {
    data2[i,y] <- 3
  }
  else if (data2[i,x] == 3) {
    data2[i,y] <- 4
  }
  else if (data2[i,x] == 4) {
    data2[i,y] <- 5
  }
  else if (data2[i,x] == 5) {
    data2[i,y] <- 6
  }
  else if (data2[i,x] == 6) {
    data2[i,y] <- 7
  }
  else if (data2[i,x] == 7) {
    data2[i,y] <- 8
  }
  else if (data2[i,x] == 8) {
    data2[i,y] <- 9
  }
  else if (data2[i,x] == 9) {
    data2[i,y] <- 10
  }
  else if (data2[i,x] == 10) {
    data2[i,y] <- 11
  }
}

I would think the following call:

borg_recode_fct("mus_base_borg_di", "mus_base_borg_di_rescale")

would work. It does not, and returns all NAs.

I realize this is a very clunky way to go about recoding. Would it be better to convert this to a factor variable and impose order? Thanks!

Use the levels argument, example:

x <- c(0, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
# [1]  0 11  1  2  3  4  5  6  7  8  9 10

factor(x, levels = c(0, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
#  [1] 0  11 1  2  3  4  5  6  7  8  9  10
# Levels: 0 11 1 2 3 4 5 6 7 8 9 10

as.numeric(factor(x, levels = c(0, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10))) - 1
# [1]  0  1  2  3  4  5  6  7  8  9 10 11

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