简体   繁体   中英

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.

I have this sample data (with 2 distinct 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?

Just as @StevenBeaupré shows, you can do it with the dplyr package. 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:

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM