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
Hello,
Here are 3 solutions, one of them the coercion to factor one.
Since you are using tibbles, I assume you also want a dplyr solution.
library(dplyr)
df1 <- tibble(dates = c(rep("2021-07-04", 2),
rep("2021-07-25", 3),
rep("2021-07-18", 4)))
You have been told how to do it. If you do not understand, you should find
a suitable tutorial to learn about how R factors work. There are some
difficulties in converting dates on an ongoing basis to factors, so I think
you should take Tom's advice to rethink this. It sounds as if you might
also d
Not if you use as.factor to convert a character type column to factor
levels. It should recode the distinct string values to factors
automatically for you.
i.e.,
df$datefactors <- as.factor(df$datestrings)
Quoting "N. F. Parsons" :
I am not averse to a factor-based solution, but I would
I am not averse to a factor-based solution, but I would still have to manually
enter that factor each month, correct? If possible, I’d just like to point R at
that column and have it do the work.
—
Nathan Parsons, B.SC, M.Sc, G.C.
Ph.D. Candidate, Dept. of Sociology, Portland State University
A
Couldn't you convert the date columns to character type data in a data
frame, and then convert those strings to factors in a 2nd step?
The only downside I think to treating dates as factor levels is that
you might have an awful lot of factors if you have a large enough
dataset.
Quoti
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)))
how in the world do I add a column that evaluates each of those dates and
assigns it a categorical value such that
datescycle
2021-07-04
12 matches
Mail list logo