[英]R give incremental value to range min-max Date, group by
I need to give an incremental value to range min-max (Date), grouped by ISBN. 我需要提供一个增量值, 范围为min-max (日期), 按 ISBN 分组 。
I have this sample data (with 2 distinct ISBN): 我有以下示例数据(带有2个不同的ISBN):
ISBN Date
67898 2013-04-01
67898 2013-05-07
67898 2014-11-21
98756 2012-02-18
98756 2014-11-07
98756 2014-11-21
And this is the result I would need: 这就是我需要的结果:
ISBN Date IncrValue
67898 2013-04-01 1
67898 2013-05-07 2
67898 2014-11-21 3
98756 2012-02-18 1
98756 2014-11-07 2
98756 2014-11-21 3
How is this possible to achive in R? 如何在R中达到目标?
Just as @StevenBeaupré shows, you can do it with the dplyr package. 就像@StevenBeaupré所示,您可以使用dplyr软件包来实现。 This is another solution using base package.
这是使用基本软件包的另一种解决方案。
# Create a toy data dataset
df <- data.frame(ISBN = rep(c(123, 456), each = 5), Date = seq(as.Date("2000-01-01"), as.Date("2013-01-01"), length.out = 10))
# dplyr solution
library(dplyr)
df %>%
group_by(ISBN) %>%
mutate(IncrValue = row_number())
# Base solution
listed <- split(df, df$ISBN) # split the data frame by the ISBN variable
newcol <- lapply(listed, function(x) {x$IncrValue <- 1:nrow(x); x}) # create a sequence column within each group
df2 <- unsplit(newcol, df$ISBN) # unsplit back together.
Try this in base
R: 在尝试此
base
R:
df$IncrValue <- as.vector(t(aggregate(Date~ISBN, df, order)[,-1]))
data 数据
df <- structure(list(ISBN = c(67898L, 67898L, 67898L, 98756L, 98756L,
98756L), Date = structure(c(15796, 15832, 16395, 15388, 16381,
16395), class = "Date")), .Names = c("ISBN", "Date"), class = "data.frame", row.names = c(NA,
-6L))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.