Reputation: 5935
I have a dataset that looks something like this:
name = c("john", "john", "john", "alex","alex", "tim", "tim", "tim", "ralph", "ralph")
year = c(2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012, 2014, 2016)
my_data = data.frame(name, year)
name year
1 john 2010
2 john 2011
3 john 2012
4 alex 2011
5 alex 2012
6 tim 2010
7 tim 2011
8 tim 2012
9 ralph 2014
10 ralph 2016
I am trying to count the "number of rows with at least one missing (i.e. non-consecutive) year", for example:
# sample output
year count
1 2014, 2016 1
In a previous question (Counting Number of Unique Column Values Per Group), I received an answer - but when I tried to apply this answer, I got the following error:
agg <- aggregate(year ~ name, my_data, c)
agg <- agg$year[sapply(agg$year, \(y) any(diff(y) != 1))]
as.data.frame(table(sapply(agg, paste, collapse = ", ")))
Error: unexpected input .... " ... \"
I think this error might be due to the fact that I am using an older version of R.
Does anyone know if an alternate symbol can be used to replace "" in R that is supported by older versions of R?
Thanks!
Upvotes: 1
Views: 72
Reputation: 887881
In tidyverse
, we may do this as
library(dplyr)
my_data %>%
group_by(name) %>%
filter(any(diff(year) != 1)) %>%
summarise(year = toString(year)) %>%
count(year, name = 'count')
-output
# A tibble: 1 × 2
year count
<chr> <int>
1 2014, 2016 1
The error in OP's code is based on the R
version. The lambda concise option (\(x)
-> function(x)
) is introduced only recently from versions R > 4.0
Upvotes: 3