(count the days by name in a date column in r and their summary)
In R, you can use the wday() function from the "lubridate" package to count the number of days for each week day name in a column of dates. This function takes a Date or POSIXct object as input and returns the week day name as output. You can then use the count() function from the "dplyr" package to count the number of days for each week day name.
For example, the following code uses the wday() and count() functions to count the number of days for each week day name in the "date" column of the "mydata" data frame:
library(lubridate)
library(dplyr)
mydata <- data.frame(date = as.Date(c("2022-01-01", "2022-02-01", "2022-03-01", "2022-03-02", "2022-03-03")))
mydata %>%
mutate(weekday = wday(date)) %>%
count(weekday)
In this example, the wday() function is applied to each value in the "date" column of the "mydata" data frame, and the resulting week day names are stored in the new "weekday" column. Then, the count() function counts the number of days for each week day name, and the resulting counts are returned in a new data frame.
Another way to count the number of days for each week day name in a column of dates is to use the dplyr pipe operator (i.e., %>%) to chain the wday() and count() functions together. For example, the following code uses the dplyr pipe operator to count the number of days for each week day name in the "date" column of the "mydata" data frame:
library(lubridate)
library(dplyr)
mydata <- data.frame(date = as.Date(c("2022-01-01", "2022-02-01", "2022-03-01", "2022-03-02", "2022-03-03")))
mydata %>%
wday(date) %>%
count()
No comments:
Post a Comment