2017-10-22 4 views
3

내 데이터 집합의 이상 치를 몇 가지 그룹화 변수로 식별하고 싶습니다. 따라서 추가 값 컬럼에 대해 FALSE/TRUE 행을 가질 것입니다. 여기서는 숫자 변수 만 포함하겠습니다.특정 입력 열에 대해서만 dplyr 파이프 내에서 lapply를 사용하여 열을 만드는 방법은 무엇입니까?

library(AER) 

    # Load Data 
    data("CigarettesSW") 

    head(CigarettesSW) 

    # state year cpi population packs income tax price taxs    
    # 1 AL 1985 1.076 3973000 116.5 46014968 32.5 102.18 33.35 
    # 2 AR 1985 1.076 2327000 128.5 26210736 37.0 101.47 37.00 
    # 3 AZ 1985 1.076 3184000 104.5 43956936 31.0 108.58 36.17 
    # 4 CA 1985 1.076 26444000 100.4 447102816 26.0 107.84 32.10 
    # 5 CO 1985 1.076 3209000 113.0 49466672 31.0 94.27 31.00 
    # 6 CT 1985 1.076 3201000 109.3 60063368 42.0 128.02 51.48 



    # Custom function 
    is_outlier <- function(x) { 
     return(x < quantile(x, 0.25) - 1.5 * IQR(x) | x > quantile(x, 0.75) + 1.5 * IQR(x)) 
    } 


    R> CigarettesSW %>% group_by(state) %>% mutate(outlier = lapply(., is_outlier)) 
    Error in mutate_impl(.data, dots) : factors are not allowed 

여기서는 숫자 변수 만 전달하려고했습니다.

R> CigarettesSW %>% group_by(state) %>% mutate_at(3:9, outlier = lapply(., is_outlier)) 
    Error in quantile.default(x, 0.25) : factors are not allowed 

그러나 오류를 반환하는 것으로 보입니다. 다르게 해결하는 방법을 모르겠습니다.

답변

3

mutate_at을 사용하는 동안 열을 순환 시키려면 lapply이 필요하지 않습니다.

CigarettesSW %>% group_by(state) %>% mutate_if(is.numeric, funs(outlier = is_outlier(.))) 
# A tibble: 96 x 16 
# Groups: state [48] 
# state year cpi population packs income tax  price  taxs cpi_outlier population_outlier packs_outlier 
# <fctr> <fctr> <dbl>  <dbl> <dbl>  <dbl> <dbl>  <dbl> <dbl>  <lgl>    <lgl>   <lgl> 
# 1  AL 1985 1.076 3973000 116.4863 46014968 32.5 102.18167 33.34834  FALSE    FALSE   FALSE 
# 2  AR 1985 1.076 2327000 128.5346 26210736 37.0 101.47500 37.00000  FALSE    FALSE   FALSE 
# 3  AZ 1985 1.076 3184000 104.5226 43956936 31.0 108.57875 36.17042  FALSE    FALSE   FALSE 
# 4  CA 1985 1.076 26444000 100.3630 447102816 26.0 107.83734 32.10400  FALSE    FALSE   FALSE 
# 5  CO 1985 1.076 3209000 112.9635 49466672 31.0 94.26666 31.00000  FALSE    FALSE   FALSE 
# 6  CT 1985 1.076 3201000 109.2784 60063368 42.0 128.02499 51.48333  FALSE    FALSE   FALSE 
# 7  DE 1985 1.076  618000 143.8511 9927301 30.0 102.49166 30.00000  FALSE    FALSE   FALSE 
# 8  FL 1985 1.076 11352000 122.1811 166919248 37.0 115.29000 42.49000  FALSE    FALSE   FALSE 
# 9  GA 1985 1.076 5963000 127.2346 78364336 28.0 97.02517 28.84183  FALSE    FALSE   FALSE 
#10  IA 1985 1.076 2830000 113.7456 37902896 34.0 101.84200 37.91700  FALSE    FALSE   FALSE 
# ... with 86 more rows, and 4 more variables: income_outlier <lgl>, tax_outlier <lgl>, price_outlier <lgl>, 
# taxs_outlier <lgl> 
: 당신이 술어로 is.numericmutate_if를 사용할 수

CigarettesSW %>% group_by(state) %>% mutate_at(3:8, funs(outlier = is_outlier(.))) 

# A tibble: 96 x 15 
# Groups: state [48] 
# state year cpi population packs income tax  price  taxs population_outlier packs_outlier 
# <fctr> <fctr> <dbl>  <dbl> <dbl>  <dbl> <dbl>  <dbl> <dbl>    <lgl>   <lgl> 
# 1  AL 1985 1.076 3973000 116.4863 46014968 32.5 102.18167 33.34834    FALSE   FALSE 
# 2  AR 1985 1.076 2327000 128.5346 26210736 37.0 101.47500 37.00000    FALSE   FALSE 
# 3  AZ 1985 1.076 3184000 104.5226 43956936 31.0 108.57875 36.17042    FALSE   FALSE 
# 4  CA 1985 1.076 26444000 100.3630 447102816 26.0 107.83734 32.10400    FALSE   FALSE 
# 5  CO 1985 1.076 3209000 112.9635 49466672 31.0 94.26666 31.00000    FALSE   FALSE 
# 6  CT 1985 1.076 3201000 109.2784 60063368 42.0 128.02499 51.48333    FALSE   FALSE 
# 7  DE 1985 1.076  618000 143.8511 9927301 30.0 102.49166 30.00000    FALSE   FALSE 
# 8  FL 1985 1.076 11352000 122.1811 166919248 37.0 115.29000 42.49000    FALSE   FALSE 
# 9  GA 1985 1.076 5963000 127.2346 78364336 28.0 97.02517 28.84183    FALSE   FALSE 
#10  IA 1985 1.076 2830000 113.7456 37902896 34.0 101.84200 37.91700    FALSE   FALSE 
# ... with 86 more rows, and 4 more variables: income_outlier <lgl>, tax_outlier <lgl>, price_outlier <lgl>, 
# taxs_outlier <lgl> 

또는 모든 숫자 컬럼에 기능을 적용 : 그냥 모든 컬럼에 적용하는 데 필요한 기능을 지정