0

How can I aggregate the dates column per month and date?

library(data.table)
df <- data.table(dates = c('2017-01-01', '2017-01-05'))

chosen_col <- 'dates'

Tried the following which does not work:

df[, .(n = .N), by =  str_sub(chosen_col, 1, 7)]
df[, .(n = .N), by =  eval(str_sub(chosen_col, 1, 7))]

EDIT:

So the question is how do I pass a character vector to a function inside the by argument. Fully aware of workarounds for the date problem.

2 Answers 2

2

You can use get():

df[, .(n = .N), by =  substring(get(chosen_col), 6)]

   substring n
1:     01-01 1
2:     01-05 1
Sign up to request clarification or add additional context in comments.

Comments

0

We can use get to use chosen_col as a column name.

library(data.table)
#Convert to actual date
df[, (chosen_col) := as.Date(get(chosen_col))]
#Separate into month and date
df[, c('month', 'date') := list(format(get(chosen_col), "%m"), 
                                format(get(chosen_col), "%d"))]

#Count number of rows for each month and date
df[, .(n = .N), .(month, date)]

1 Comment

data.table also has functions wday() and month() (which could be used instead of format).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.