Thank you all so much for your time and your help! I am truly grateful for
the suggested solutions, but more importantly, for the lessons!
Nate Parsons
On Thu, Jul 22, 2021 at 4:13 AM Eric Berger wrote:
> While the base R solution using 'factor' appears to win based on elegance,
> chapeau to t
While the base R solution using 'factor' appears to win based on elegance,
chapeau to the creativity of the other suggestions.
For those who are not aware, R 4.1.0 introduced two features: (1) native
pipe |> and (2) new shorter syntax for anonymous functions.
Erich's suggestion used the native pipe
For a data.frame d, I'd simply do
d$cycle <- factor(d$dates, labels=1:3)
but I have not idea about tibbles.
Best,
Uwe Ligges
On 22.07.2021 05:12, N. F. Parsons wrote:
Hi all,
If I have a tibble as follows:
tibble(dates = c(rep("2021-07-04", 2), rep("2021-07-25", 3),
rep("2021-07-18", 4)
Hello,
Great function!
Here is a simplified, (hopefully) more general version.
seq_from_group <- function(x){
x |>
unique() |>
sort() |> (\(ranks) match(x, ranks))()
}
date_df |>
mutate(cycle = seq_from_group(dates))
With ?interaction it works with more than one column.
df2 <
I had no idea that ‘cur_group_id()’ existed!?!! Will definitely try that. Thank
you!!!
—
Nathan Parsons, B.SC, M.Sc, G.C.
Ph.D. Candidate, Dept. of Sociology, Portland State University
Adjunct Professor, Dept. of Sociology, Washington State University
Graduate Advocate, American Association of U
date_df <- tibble(dates = c(rep("2021-07-04", 2), rep("2021-07-25", 3),
rep("2021-07-18", 4)))
cycle_from_date <- function(date,dates){
dates |>
unique() |>
sort() ->
ranks
match(date,ranks)
}
date_df |>
mutate(cycle_new=cycle_from_date(dates,dates))
> On 22.07.2021, at 05
6 matches
Mail list logo