# Counting weekday in a survey

Just downloaded RStudio as I have a pending survey coming in. Below is a test. We asked what days people would prefer, and the data will come in Monday;Tuesday or Tuesday;Wednesday;Friday. After splitting it up and learning about tables, I have the below. My question: I'd like to count the Monday's, Tuesday's, etc. to see the grand total for each workday. I can't seem to figure this out. Of course, I'll want to plot this...but feel I need to count them up first. Thought I'd ask. Thanks so much

> table(swing\$Days.WFH.swingspace.1)

Monday  Thursday   Tuesday Wednesday
1             1                 2         2
> table(swing\$Days.WFH.swingspace.2)

Friday  Thursday Wednesday
2            3         1
> table(swing\$Days.WFH.swingspace.3)

Friday Thursday
1        1

I assume your data looks similar to my df below.

Please note that it would be easier to help if you could post some sample data (using dput()), if you provided a reprex, and/or if you provided the desired output. For more info on that, please see the FAQ.

It might help you, that you are dealing with ranked or ranking data. For example, this brings up the following question: How to analyase ranking data in R please?

Finally, here is some code do deal with your data. Therein, I computed the average rank for each day and then plotted this average rank. I hope this leads you in the right direction. If not, please provide details about the desired result.

library("tidyverse")

set.seed(1)

days <- c("Mon", "Tue", "Wed", "Thu", "Fri")

# generate example data frame df
df <- data.frame(pers = 1:10,
resp = replicate(10, paste(sample(days), collapse = "; ")))

#>   pers                    resp
#> 1    1 Mon; Thu; Wed; Fri; Tue
#> 2    2 Fri; Wed; Thu; Tue; Mon
#> 3    3 Wed; Fri; Mon; Thu; Tue
#> 4    4 Tue; Fri; Thu; Wed; Mon
#> 5    5 Mon; Wed; Fri; Thu; Tue
#> 6    6 Mon; Tue; Fri; Wed; Thu

res <- separate(df, col = resp, into = paste0("rank", 1:5)) %>%
pivot_longer(cols = -pers, values_to = "day") %>%
mutate(rank = as.numeric(sub("rank", "", name)),
day = factor(day, levels = days)) %>%
group_by(day) %>%
summarize(M = mean(rank))

res
#> # A tibble: 5 x 2
#>   day       M
#>   <fct> <dbl>
#> 1 Mon     2.8
#> 2 Tue     3
#> 3 Wed     3.1
#> 4 Thu     3.2
#> 5 Fri     2.9

ggplot(res, aes(x = day, y = M)) +
geom_col() +
labs(y = "Mean rank")

Thank you. I'll give this a shot. Your example looks very much like my CSV file from the pre-test. I should have confirmed, this is not ranking but simply asks people to select days via list. My data comes out just like yours e.g. Monday;Friday. Thank you.

I'm clearly missing something. I have test data to share. Question 5 of survey simply asks what days would you like to work from home, select all that apply (M-F and No Preference). If I did this correctly, you will see six people who have done the pre-test. Because the test data was so small, I split it up prior to bringing it into R (I suspect no more than 200 people will be taking this).

As you can see, I have NA's as some people will only work from home x2 a week, others like x3.

The goal is to do what you did, plot a bar chart and see the what people think. Perhaps this is what you were looking for? Thanks again. Appreciate any info.

Testy

# A tibble: 6 x 4

ID Q5.1      Q5.2      Q5.3

1 14 Wednesday Thursday NA
2 15 Monday Thursday Friday
3 16 Thursday Friday NA
4 17 Tuesday Thursday NA
5 18 Tuesday Wednesday Thursday
6 19 Wednesday Friday NA

dput(Testy, file = "",

•  control = c("keepNA", "keepInteger", "niceNames", "showAttributes"))

structure(list(ID = c(14, 15, 16, 17, 18, 19), Q5.1 = c("Wednesday",
"Monday", "Thursday", "Tuesday", "Tuesday", "Wednesday"), Q5.2 = c("Thursday",
"Thursday", "Friday", "Thursday", "Wednesday", "Friday"), Q5.3 = c(NA,
"Friday", NA, NA, "Thursday", NA)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L), spec = structure(list(
cols = list(ID = structure(list(), class = c("collector_double",
"collector")), Q5.1 = structure(list(), class = c("collector_character",
"collector")), Q5.2 = structure(list(), class = c("collector_character",
"collector")), Q5.3 = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))

@ramrod97, I understand it's not ranking data. Thus, calculating the mean rank for each day as in my plot above does not make sense. Rather, you can just count how often each day was selected.

In the code below, I explicitely used intermediate steps (rather than piping it all together with >%>) so that you can follow along more easily.

library("dplyr")
library("tidyr")
library("ggplot2")
Testy <- structure(list(ID = 14:19,
Q5.1 = c("Wednesday", "Monday", "Thursday", "Tuesday",
"Tuesday", "Wednesday"),
Q5.2 = c("Thursday", "Thursday", "Friday", "Thursday",
"Wednesday", "Friday"),
Q5.3 = c(NA, "Friday", NA, NA, "Thursday", NA)),
class = "data.frame", row.names = c(NA, -6L))

Testy_long_format <- pivot_longer(Testy, cols = -ID, values_to = "day", values_drop_na = TRUE)
Testy_long_format\$day <- factor(Testy_long_format\$day,
levels = c("Monday", "Tuesday", "Wednesday",
"Thursday", "Friday"))
Testy_summarized <- count(Testy_long_format, day)
# Alternative to count(): group_by(Testy_long_format, day) %>% summarize(n = n())

ggplot(Testy_summarized, aes(x = day, y = n)) +
geom_col()

If you have further questions, please make sure to format your code blocks appropriately such that they can be read and copied more easily: FAQ: How to format your code.

Amazing. Thank you so much. Appreciate the patience and help.

1 Like

A post was split to a new topic: Getting started with non-metric multidimensional scaling in R

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.