1

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!

1
  • 1
    Read about factor function, you can set order of levels. Commented Aug 14, 2018 at 10:31

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.