1

I would like to do a simple if statement to group codes into groups. The variable has number codes and I would like to create a new variable that groups several number codes together. I have written the following if statement but because they are many code numbers (30 codes), I need help writing a more elegant code to group the variable rather than writing 30+ if statements.

Data2$RevisedSIC.Group <-c()
for (i in 1:length(Data2$SIC.Group )) {
if (Data2$SIC.Group[i] =="10110") Data2$RevisedSIC.Group [i]="Metal" else 
if (Data2$SIC.Group[i] =="10410") Data2$RevisedSIC.Group [i]="Metal" else
if (Data2$SIC.Group[i] =="10439") Data2$RevisedSIC.Group [i]="Metal" else
if (Data2$SIC.Group[i] =="14111") Data2$RevisedSIC.Group [i]="Stone" else
if (Data2$SIC.Group[i] =="10421") Data2$RevisedSIC.Group [i]="Stone" }

3 Answers 3

1

There is no need for a loop

Data2 <-data.frame(rep(c(10110,10410,10439,14111),2))
colnames(Data2) <-"SIC.Group"

Data2$RevisedSIC.Group[Data2$SIC.Group %in% c(10110,10410,10439)] <- "Metal"
Data2$RevisedSIC.Group[Data2$SIC.Group %in% 14111] <- "Stone"

  SIC.Group RevisedSIC.Group
1     10110            Metal
2     10410            Metal
3     10439            Metal
4     14111            Stone
5     10110            Metal
6     10410            Metal
7     10439            Metal
8     14111            Stone
Sign up to request clarification or add additional context in comments.

Comments

1

take a look at match

lookup <- data.frame(code=c('10110','10410','10439','14111','10421'), name=c('Metal','Metal','Metal','Stone','Stone'))

Data2$RevisedSIC.Group <- lookup$name[match(Data2$SIC.Group,lookup$code)]

1 Comment

Thank you everyone for your time and answers!
0

You can use %in%:

metals <- c("10110","10410","10439")
stones <- c("14111","10421")
# ... and so on for each group

if ( Data2$SIC.Group[i] %in% metals ) {
    Data2$SIC.Group[i] <- "Metal"
} else if ( Data2$SIC.Group[i] %in% stones ) {
    Data2$SIC.Group[i] <- "Stone"

}     # ... and so on for each group

You'd still need to write as many if/else as there are groups, which is hopefully much less than the number of codes you have.

To condense it further you could dispense with the loop:

metals <- c("10110","10410","10439")
stones <- c("14111","10421")
# ... and so on for each group

Data2$SIC.Group[ Data2$SIC.Group %in% metals ] <- "Metal"
Data2$SIC.Group[ Data2$SIC.Group %in% stones ] <- "Stone"
# ... and so on for each group

To condense even further, you can define your groups in a list as below and then just have one line of code for all groups in the assigning step:

groupCodes <- list(
   metals=c("10110","10410","10439"),
   stones=c("14111","10421")
   # ... and so on for each group
)

for (n in names(groupCodes)) {
    # just once for all groups.
    Data2$SIC.Group[ Data2$SIC.Group %in% groupCodes[[n]] ] <- n
}

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.