0

I have a variable (V7) in dataset (total observation is 137) range from 0 to 100, I would like to create a new variable (V12) which it group the variable (V7) into 3 groups with 75-100,60-74, 0-59. I use

if(data$V7>="75"){
  V12 <- 1
} else {
  if(data$V7>="60"){
    V12 <- 2
  } else {
    V12 <- 3
  }
}

but I get Warning messages: 1: In if (data$V7 >= "75") { : the condition has length > 1 and only the first element will be used 2: In if (data$V7 >= "60") { : the condition has length > 1 and only the first element will be used

Anyone could help me on this matter? Thanks!

2
  • 3
    What programming language is this? You should tag your question with the language in use. Commented Dec 5, 2013 at 1:35
  • jlhoward, thanks! I manage to group it to the intervals, but how do I create the new variable for this group? Because I need it for further statistics. Commented Dec 5, 2013 at 2:03

2 Answers 2

3
set.seed(1)      # so this is reproducible
df     <- data.frame(V7=sample(0:100,50))
df$V12 <- 0
df$V12 <- findInterval(df$V7,c(0,60,75,100))
Sign up to request clarification or add additional context in comments.

1 Comment

jlhoward, I get it. Thank you very much! :)
0

If I understood your question properly, you want to make three groups from a dataframe.

Let's assume you have 2 vectors. Let's create some random numbers and make your data

require(stats)
V7 <- runif(137, min=0, max=110)
V1 <- runif(137, min=0, max=10)
data<-data.frame(V1,V7)

class 75-100

g1 = data[data$V7 >= 75  & data$V7 <= 100,]
g1$V7

class 60-74

g2=data[data$V7 >= 60 & data$V7 <= 74,]
g2$V7

class 0-59

g3 =data[data$V7 >= 0 & data$V7 <= 59,]
g3$V7

1 Comment

Hi ToNoy, thanks! The dataset is actually with total of 11 variables, one of the variable are range from 0 - 100, I want to group them into 3 intervals and create a new variable for it. Your solution is very useful for me as later I need this knowledge to run simulation as well. Thank you so much! :)

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.