什么是“ .N”?在R的数据表中意味着什么? [英] What does ".N" means in data table in r?
问题描述
我有一个数据表 dt
:
library(data.table)
dt = data.table(a=LETTERS[c(1,1:3)],b=4:7)
a b
1: A 4
2: A 5
3: B 6
4: C 7
dt [,.N,by = a]
的结果是
a N
1: A 2
2: B 1
3: C 1
我知道 by = a
或 by = a
表示按 a
列和 N
列是 a $ c $重复时间的总和c>。但是,我没有使用
nrow()
,但是得到了结果。 .N
不仅仅是列名吗?我在R中找不到 ??。N
的文档。我尝试使用 .K
,但这不起作用。 .N
是什么意思?
I know the by=a
or by="a"
means grouped by a
column and the N
column is the sum of duplicated times of a
. However, I don't use nrow()
but I got the result. The .N
is not just the column name? I can't find the document by ??".N"
in R. I tried to use .K
, but it doesn't work. What does .N
means?
推荐答案
将 .N
视为实例数量的变量。例如:
Think of .N
as a variable for the number of instances. For example:
dt <- data.table(a = LETTERS[c(1,1:3)], b = 4:7)
dt[.N] # returns the last row
# a b
# 1: C 7
您的示例返回一个新变量,其中包含每种情况下的行数:
Your example returns a new variable with the number of rows per case:
dt[, new_var := .N, by = a]
dt
# a b new_var
# 1: A 4 2 # 2 'A's
# 2: A 5 2
# 3: B 6 1 # 1 'B'
# 4: C 7 1 # 1 'C'
有关data.table的所有特殊符号的列表,另请参见 https://www.rdocumentation.org/packages/data.table/versions/1.10.0/topics/special-symbols
For a list of all special symbols of data.table, see also https://www.rdocumentation.org/packages/data.table/versions/1.10.0/topics/special-symbols
这篇关于什么是“ .N”?在R的数据表中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!