Loop through data.table and create new columns basis some condition

后端 未结 3 1939
无人共我
无人共我 2021-01-13 04:34

I have a data.table with quite a few columns. I need to loop through them and create new columns using some condition. Currently I am writing separate line of condition for

3条回答
  •  抹茶落季
    2021-01-13 05:23

    Your data should probably be in long format:

    m = melt(DT, id=c("town","tc"))
    

    Then just write your test once

    m[, 
      is_outlier := +(abs(value-mean(value)) > 1.96*sd(value))
    , by=.(town, tc, variable)]
    

    I see no outliers in this data (according to the given definition of outlier):

    m[, .N, by=is_outlier] # this is a handy alternative to table()
    
    #    is_outlier   N
    # 1:          0 160
    

    How it works

    • melt keeps the id columns and stacks all the rest into
      • variable (column names)
      • value (column contents)
    • +x does the same thing as as.integer(x), coercing TRUE/FALSE to 1/0

    If you really like your data in wide format, though:

    vjs = setdiff(names(DT), c("town","tc"))
    DT[, 
      paste0(vjs,".out") := lapply(.SD, function(x) +(abs(x-mean(x)) > 1.96*sd(x)))
    , by=.(town, tc), .SDcols=vjs]
    

提交回复
热议问题