I have a time series dataset that contains a column year
, week
and a third numeric value. From this dataset, I want to remove the last week from the last year as it is incomplete.
My attempt:
subset(data , year== max(as.numeric(as.character(data$year))) & week == max(data$week))
However, the problem with this code, is that it returns an empty dataframe, since the maximum week number, in the subset of the maximum year, does not exist.
How can I achieve this?
UPDATE: here is a link with a sample of the data I am using
We could arrange
the data based on year
and week
and remove the week
and year
value which comes on the last line of the data.
library(dplyr)
data %>%
type.convert(as.is = TRUE) %>%
arrange(year, week) %>%
filter(!(year == last(year) & week == last(week)))
Same logic in base R:
data <- type.convert(with(data, order(year, week)), as.is = TRUE)
subset(data, !(year == year[nrow(data)] & week == week[nrow(data)]))
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.