如何理解data.table中。()的含义

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何理解data.table中。()的含义相关的知识,希望对你有一定的参考价值。

我对data.table中的。()感到困惑。很难在Google中搜索。()。那么,谁能详细解释一下R data.table中的。()是什么?谢谢!

我想显示一些代码以更好地进行澄清:

df = data.table(col1 = c(1:20), col2 = seq(1, 100, by = 5), col3 = c(replicate(10, "A"), replicate(10, "B")))

df %>% group_by(col3) %>% summarise(mean = mean(col2)) %>% View() `This is what I want in data.table`

df %>% .[, .(mean = mean(col2)), by = col3] %>% View()`# this code is what I want, but it can only run with .(), as I have only one column(col2) in j, my question is why we need .() here?`

df %>% .[, mean := mean(col2), by = col3] %>% View() `this is not what I want as it creates duplicated rows`
答案

。()部分只是list()的别名,如果愿意,可以改用list()。您可以使用任何R包放置列名称的任何R表达式,并在此处返回不同类型的不同长度。e。 g以下代码告诉R选择“来源”,“年”,“月”,“小时”列:

数据= mydata [,。(来源,年,月,小时)]

以上是关于如何理解data.table中。()的含义的主要内容,如果未能解决你的问题,请参考以下文章