建立指标 [英] Create indicator
问题描述
我想为矩阵创建一个数字指示器,以便对于一个变量中的每个唯一元素,它根据另一个变量中的元素创建长度序列.例如:
I would like to create a numeric indicator for a matrix such that for each unique element in one variable, it creates a sequence of the length based on the element in another variable. For example:
frame<- data.frame(x = c("a", "a", "a", "b", "b"), y = c(3,3,3,2,2))
frame
x y
1 a 3
2 a 3
3 a 3
4 b 2
5 b 2
指标z应该看起来像这样:
The indicator, z, should look like this:
x y z
1 a 3 1
2 a 3 2
3 a 3 3
4 b 2 1
5 b 2 2
任何人和所有帮助都将不胜感激.谢谢.
Any and all help greatly appreciated. Thanks.
推荐答案
否ave
?
frame$z <- with(frame, ave(y,x,FUN=seq_along) )
frame
# x y z
#1 a 3 1
#2 a 3 2
#3 a 3 3
#4 b 2 1
#5 b 2 2
data.table
版本可能类似于以下内容(感谢@mnel):
A data.table
version could be something like below (thanks to @mnel):
#library(data.table)
#frame <- as.data.table(frame)
frame[,z := seq_len(.N), by=x]
我最初的想法是使用:
frame[,z := .SD[,.I], by=x]
其中,.SD
是指data.table
拆分by
x
的每个子集. .I
返回整个data.table
的行号.因此,.SD[,.I]
返回每个组内的行号.尽管@mnel指出,与其他方法相比,这种方法效率低下,因为需要为每个组将整个.SD
加载到内存中才能运行此计算.
where .SD
refers to each subset of the data.table
split by
x
. .I
returns the row numbers for an entire data.table
. So, .SD[,.I]
returns the row numbers within each group. Although, as @mnel points out, this is inefficient compared to the other method as the entire .SD
needs to be loaded into memory for each group to run this calculation.
这篇关于建立指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!