2

I'm having a little issue. Relatively new to R.

What I am trying to do is to create a new variable (data$V4) based on the value of an existing variable (data$V3).

The condition is that if the value in variable 3 is within a certain range (e.g., >=0 & <=100), then the value in data$V4 = 1. There need to be 20 statements exactly of this sort.

I have created a code which works only for two expressions, it does not go beyond this for some reason. I don't seem to be closing the statement.

data$V4 <- ifelse((data$V3>=0) & (data$V3<=100), 1, ifelse((data$V3>=101) & (data$V3<=400), 2,0)

This code works fine. But adding more expressions with the code below fails.

data$V4 <- ifelse((data$V3>=0) & (data$V3<=100), 1,
              ifelse((data$V3>=101) & (data$V3<=400), 2,
                     ifelse((data$V3>=401) & (data$V3<=800), 3,
                            ifelse((data$V3>=801) & (data$V3<=1200), 4,0))
JohnDoe
  • 21
  • 1
  • You have 12 left parens `(` and only 10 right parens `)`. They need to be balanced. But for stuff like this you should be using `cut` rather than nested `ifelse` - it will be much easier. – Gregor Thomas Jul 31 '17 at 18:05
  • I think the [findInterval](https://stat.ethz.ch/R-manual/R-devel/library/base/html/findInterval.html) function is better suited here than the cut function. Simply do `data$V4 = findInterval(data$V3, c(0,100,400,800,1200))`, this returns what you want directly rather than having to specify labels. Also it returns numerical values as requested. – Florian Jul 31 '17 at 18:07
  • ```V3 <- c(50, 100, 150, 400, 600, 800, 900, 1500) df <- data.frame(V3) for(i in 1:nrow(df)){ if (df$V3[i]<=100) { df$V4[i] <- 1 } else if ((df$V3[i]>101) & (df$V3[i]<=400)) { df$V4[i] <- 2 } else if ((df$V3[i]>401) & (df$V3[i]<=800)) { df$V4[i] <- 3 } else if (df$V3[i]<=1200) { df$V4[i] <- 4 } else { df$V4[i] <- 0 } } ``` – pyll Jul 31 '17 at 18:46

0 Answers0