2016-04-11 12 views
0

böyle bakan bir işlevi oluşturmak çalışılıyor:Çoklu Elseif Beyanı

region <- function(State){ 
region_vector <- 
    ifelse(State %in% c("CT", "DE", "DC", "MD", "ME", "MD", "MA", "NH","NJ", "NY", "PA", "RI", "VT"), "Northeast", 
    ifelse(State %in% c("IN", "KY", "MI", "OH", "PA", "WV") "East Central", 
    ifelse(State %in% c("IN", "KY", "MI", "OH", "PA", "WV") "East Central", 
    ifelse(State %in% c("CO", "IL", "IA", "KS", "MN", "MO", "MT", "NE", "ND", "SD", "WI", "WY") "West Central", 
    ifelse(State %in% c("AL", "FL", "GA", "MS", "NC", "SC", "TN", "VA") "Southeast", 
    ifelse(State %in% c("AR", "LA", "NM", "OK", "TC") "Southwest", 
    ifelse(State %in% c("AK", "AZ", "CA", "HI", "ID", "NV", "OR", "UT", "WA") "Pacific", 
      "NA"))))))) 
return(region_vector) } 

Ben c ("CA", "NY", "CO"...) benziyor benim verilerindeki bir sütuna bölgeleri atamak istiyorum ben ettik kod sorununun ne yukarıda yazılı? Hata mesajı Sadece göstermelik unexpected string constant in c (...)

+1

Doğu Central hem de. '' Switch''e bakın. Hatanız için, c (...) ile c (...) içeriğini değiştirmek istediğiniz arasındaki virgülleri kaçırıyorsunuz. Ayrıca, seni bilmiyorum, ama çok fazla ifelse seni dertte sokabilir! – infominer

+2

'switch'? Hayır, bir arama masasına ve sonra 'birleştirme' ye ihtiyacınız var. – Gregor

cevap

1

olduğunu diğer Bunu yapmanın yolu ve nasıl slooow ifelse, bazı örnekler:

library("microbenchmark") 
microbenchmark(match_recode(test$CODE, dico = L), 
       region(test$CODE), 
       dt_recode(test$CODE, dico = df), 
       times = 100L) 
# Unit: microseconds 
#        expr  min  lq  mean  median   uq  max neval 
# match_recode(test$CODE, dico = L) 266.845 271.549 344.7044 288.2265 298.7035 1138.792 100 
# region(test$CODE)     23454.496 24250.325 26391.6468 24637.9750 25257.4050 49958.884 100 
# dt_recode(test$CODE, dico = df) 1133.233 1184.977 1355.1031 1364.3705 1445.8345 2116.794 100 
:

# Lookup list 
l <- list(
    "Northeast" = c("CT", "DE", "DC", "MD", "ME", "MA", "NH","NJ", "NY", "RI", "VT"), 
    "East central" = c("IN", "KY", "MI", "OH", "PA", "WV"), 
    "West central" = c("CO", "IL", "IA", "KS", "MN", "MO", "MT", "NE", "ND", "SD", "WI", "WY"), 
    "Southeast" = c("AL", "FL", "GA", "MS", "NC", "SC", "TN", "VA"), 
    "Southwest" = c("AR", "LA", "NM", "OK", "TC"), 
    "Pacific" = c("AK", "AZ", "CA", "HI", "ID", "NV", "OR", "UT", "WA") 
) 
# long list 
L <- unlist(l) 
names(L) <- rep(names(l), times = lapply(l, length)) 
# data.frame 
df <- data.frame(
    CODE = unlist(l), 
    LABEL = rep(names(l), times = lapply(l, length)), 
    stringsAsFactors = FALSE, row.names = NULL 
) 

# Test data 
set.seed(123) 
test <- data.frame(CODE = sample(x = unlist(l), size = 1e4, replace = TRUE), stringsAsFactors = FALSE) 


# Fun to recode with match 
match_recode <- function(var, dico) { 
    names(dico)[match(x = var, table = dico)] 
} 

# With ifelse 
region <- function(State){ 
    region_vector <- 
    ifelse(State %in% c("CT", "DE", "DC", "MD", "ME", "MA", "NH","NJ", "NY", "RI", "VT"), "Northeast", 
      ifelse(State %in% c("IN", "KY", "MI", "OH", "PA", "WV"), "East Central", 
        ifelse(State %in% c("IN", "KY", "MI", "OH", "PA", "WV"), "East Central", 
         ifelse(State %in% c("CO", "IL", "IA", "KS", "MN", "MO", "MT", "NE", "ND", "SD", "WI", "WY"), "West Central", 
           ifelse(State %in% c("AL", "FL", "GA", "MS", "NC", "SC", "TN", "VA"), "Southeast", 
             ifelse(State %in% c("AR", "LA", "NM", "OK", "TC"), "Southwest", 
               ifelse(State %in% c("AK", "AZ", "CA", "HI", "ID", "NV", "OR", "UT", "WA"), "Pacific", 
                "NA"))))))) 
    return(region_vector) 
} 

# With data.table 
dt_recode <- function(var, dico) { 
    dt <- data.table(CODE = var) 
    setkey(dt, CODE) 
    dt <- dt[dico] 
    return(dt$LABEL) 
} 

Testi

table(match_recode(test$CODE, dico = L)) 
# East central Northeast  Pacific Southeast Southwest West central 
#   1211   2132   1711   1554   998   2394 
table(region(test$CODE)) 
# East central Northeast  Pacific Southeast Southwest West central 
#   1211   2132   1711   1554   998   2394 
library("data.table") 
table(dt_recode(test$CODE, dico = df)) 
# East central Northeast  Pacific Southeast Southwest West central 
#   1211   2132   1711   1554   998   2394 

# All the same 

Benchmark sonuçları

match, 012'den çok daha hızlıdır!

muhtemelen data.table

PS ile daha iyi bir yolu var: seninki ifelse, MD kuzeydoğuda iki kez daha görüntüye, PA kuzeydoğuda ve bu switch` `için iyi bir durum gibi görünüyor