[英]What does “.N” means in data table in r?
I have a data table dt
: 我有一个数据表
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
The result of dt[, .N, by=a]
is dt[, .N, by=a]
是
a N
1: A 2
2: B 1
3: C 1
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
. 我知道
by=a
或by="a"
是指通过分组a
列和N
列是重复的时间之和a
。 However, I don't use nrow()
but I got the result. 但是,我不使用
nrow()
但我得到了结果。 The .N
is not just the column name? .N
不只是列名吗? I can't find the document by ??".N"
in R. I tried to use .K
, but it doesn't work. 我在R.中找不到文件
??".N"
我尝试使用.K
,但它不起作用。 What does .N
means? 什么
.N
意味着什么?
Think of .N
as a variable for the number of instances. 将
.N
视为实例数的变量。 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'
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 有关data.table的所有特殊符号的列表,另请参阅https://www.rdocumentation.org/packages/data.table/versions/1.10.0/topics/special-symbols
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.