创建具有多个变量的计数器 [英] Create counter with multiple variables
问题描述
我的数据如下所示:
CustomerID TripDate
1 1/3/2013
1 1/4/2013
1 1/9/2013
2 2/1/2013
2 2/4/2013
3 1/2/2013
我需要创建一个计数器变量,如下所示:
I need to create a counter variable, which will be like below:
CustomerID TripDate TripCounter
1 1/3/2013 1
1 1/4/2013 2
1 1/9/2013 3
2 2/1/2013 1
2 2/4/2013 2
3 1/2/2013 1
Tripcounter
将用于每个客户。
推荐答案
使用 ave
。假设您的 data.frame
被称为 mydf:
Use ave
. Assuming your data.frame
is called "mydf":
mydf$counter <- with(mydf, ave(CustomerID, CustomerID, FUN = seq_along))
mydf
# CustomerID TripDate counter
# 1 1 1/3/2013 1
# 2 1 1/4/2013 2
# 3 1 1/9/2013 3
# 4 2 2/1/2013 1
# 5 2 2/4/2013 2
# 6 3 1/2/2013 1
对于它的价值,我还实现了我的 splitstackshape包中包含的函数中采用了这种方法。该函数称为 getanID
:
For what it's worth, I also implemented a version of this approach in a function included in my "splitstackshape" package. The function is called getanID
:
mydf <- data.frame(IDA = c("a", "a", "a", "b", "b", "b", "b"),
IDB = c(1, 2, 1, 1, 2, 2, 2), values = 1:7)
mydf
# install.packages("splitstackshape")
library(splitstackshape)
# getanID(mydf, id.vars = c("IDA", "IDB"))
getanID(mydf, id.vars = 1:2)
# IDA IDB values .id
# 1 a 1 1 1
# 2 a 2 2 1
# 3 a 1 3 2
# 4 b 1 4 1
# 5 b 2 5 1
# 6 b 2 6 2
# 7 b 2 7 3
如上例所示,编写该函数的方式是可以指定一个或多个应视为ID列的列。它会检查 id.vars
是否重复,如果重复,则会为您生成一个新的ID变量。
As you can see from the example above, I've written the function in such a way that you can specify one or more columns that should be treated as ID columns. It checks to see if any of the id.vars
are duplicated, and if they are, then it generates a new ID variable for you.
这篇关于创建具有多个变量的计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!