using mean with .SD and .SDcols in data.table

前端 未结 1 322
北海茫月
北海茫月 2020-12-16 05:15

I am writing a very simple function to summarize columns of data.tables. I am passing one column at a time to the function, and then doing some diagnostics to figure out th

相关标签:
1条回答
  • 2020-12-16 06:07

    I think the appropriate way of approaching what you want is to just use the standard syntax:

    dt[ , lapply(.SD, mean), .SDcols = "a"]

    Alternatively, you can pass a variable by name as follows:

    col_to_pass = "a"
    dt[ , mean(get(col_to_pass)) ]
    

    Eventually, you can generalized this approach to multiple columns as follows:

    col_to_pass = c("a", "d")
    dt[ , lapply( mget(col_to_pass), mean) ]
    
    0 讨论(0)
提交回复
热议问题