This is a common case as far as I can see. Make a table with variables activity where activity is a number from 1 to 5 and dates using date to transform in day names and then summarize how many days have what of the amount of activity.
In R, you can use the wday() function from the "lubridate" package to transform a column of dates into a column of week day names. You can then use the dplyr package to create a table with variables "activity" and "weekday", where "activity" is a number from 1 to 10 and "weekday" is the week day name for the corresponding date. Finally, you can use the summarize() function from the "dplyr" package to summarize the number of days for each activity.
For example, the following code uses the wday() and summarize() functions to create a table with variables "activity" and "weekday", and to summarize the number of days for each activity:
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")),
activity = c(1, 2, 3, 4, 5))
mydata %>%
mutate(weekday = wday(date)) %>%
group_by(activity, weekday) %>%
summarize(count = n())
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 group_by() function groups the data by the "activity" and "weekday" variables, and the summarize() function counts the number of days for each group.
For example, the following code uses the wday() and summarize() functions to create a table with variables "activity" and "weekday", and to summarize the number of days for each activity:
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")),
activity = c(1, 2, 3, 4, 5))
mydata %>%
mutate(weekday = wday(date)) %>%
group_by(activity, weekday) %>%
summarize(count = n())
The resulting table shows the number of days for each activity and week day name, as shown below:
- activity weekday count
- 1 Saturday 1
- 2 Sunday 1
- 3 Wednesday 2
- 4 Wednesday 1
- 5 Wednesday 1
In this example, there is one day with activity 1 on Saturday, one day with activity 2 on Sunday, two days with activity 3 on Wednesday, one day with activity 4 on Wednesday, and one day with activity 5 on Wednesday.
and then...
transform activity number from the above table to values 1=very low activity, 2=low activity, 3=medium activity, 4=good activity, 5=strong activity
In R, you can use the mutate() function from the "dplyr" package to transform the values of a column in a data frame. For example, the following code uses the mutate() function to transform the "activity" column of the "mydata" data frame from numbers 1 to 5 to the corresponding activity level:
library(dplyr)
mydata <- data.frame(activity = c(1, 2, 3, 4, 5))
mydata %>%
mutate(activity = case_when(activity == 1 ~ "very low activity",
activity == 2 ~ "low activity",
activity == 3 ~ "medium activity",
activity == 4 ~ "good activity",
activity == 5 ~ "strong activity"))
In this example, the mutate() function uses the case_when() function to replace the values in the "activity" column with the corresponding activity level. The resulting data frame has the transformed "activity" column, as shown below:
- activity
- very low activity
- low activity
- medium activity
- good activity
- strong activity
In conclusion, the mutate() function from the "dplyr" package is a useful tool for transforming the values of a column in a data frame. You can use the case_when() function within the mutate() function to specify the transformation rules for each value in the column.
In R, you can use the wday() function from the "lubridate" package to transform a column of dates into a column of week day names. You can then use the dplyr package to create a table with variables "activity", "weekday", and "count", where "activity" is the activity level, "weekday" is the week day name for the corresponding date, and "count" is the number of days for each activity and week day name.
For example, the following code uses the wday() and summarize() functions to create a table with variables "activity", "weekday", and "count", and to summarize the number of days for each activity and week day name:
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")),
activity = c(1, 2, 3, 4, 5))
mydata %>%
mutate(weekday = wday(date),
activity = case_when(activity == 1 ~ "very low activity",
activity == 2 ~ "low activity",
activity == 3 ~ "medium activity",
activity == 4 ~ "good activity",
activity == 5 ~ "strong activity")) %>%
group_by(activity, weekday) %>%
summarize(count = n())
The resulting table shows the number of days for each activity and week day name, as shown below:
show the table above
The resulting table shows the number of days for each activity and week day name, as shown below:
In this example, there is one day with very low activity on Saturday, one day with low activity on Sunday, two days with medium activity on Wednesday, one day with good activity on Wednesday, and one day with strong activity on Wednesday.
In conclusion, the wday() and summarize() functions from the "lubridate" and "dplyr" packages are useful tools for creating a table with variables "activity", "weekday", and "count", and for summarizing the number of days for each activity and week day name in R.