2012-01-25 74 views
1

我想做一個簡單的if語句來將代碼分組到組中。該變量有數字代碼,我想創建一個新的變量,將幾個數字代碼組合在一起。我已經寫了如下陳述,但因爲它們有很多代碼(30個代碼),我需要幫助編寫一個更優雅的代碼來對變量進行分組,而不是編寫30+條語句。使用if語句在R中分組變量

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" } 

回答

1

ŧ這裏沒有必要爲一個循環

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 
0

您可以使用%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" 

} #...等等每個組

你還是會需要編寫,如果儘可能多/別的,因爲有組,這比希望的數量少得多。

進一步壓縮它,你可以與循環分配:

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 

爲了進一步凝聚,您可以在下面的列表定義組,然後只需要一行代碼在所有組分配步驟:

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 
} 
1

看看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)] 
+0

謝謝大家對你的時間和答案! – Amateur