Rearrangement of Data

Hi
I have a dataset with teacher names and their support for a particular program. Their support is gauged wee-wise as shown in the sample data given below. I want to get the frequency of different support scales. Let us say the first teacher Nithin. I want know how many times he gave support for each scale. That will look like the data frame "data2"as shown below. How can I do this?

library(tidyverse)
library(janitor)

data<-tibble::tribble(
  ~teacher_name, ~tch_support, ~week,
       "Nithin",         0.25,    1L,
      "Supriya",          0.5,    1L,
      "Sunitha",         0.75,    1L,
      "Pradeep",          0.5,    1L,
    "Madhumati",          0.5,    1L,
    "Prathibha",          0.5,    1L,
       "Nithin",          0.5,    2L,
      "Supriya",         0.25,    2L,
      "Sunitha",         0.25,    2L,
      "Pradeep",         0.25,    2L,
    "Madhumati",         0.75,    2L,
    "Prathibha",         0.75,    2L,
       "Nithin",         0.75,    3L,
      "Supriya",          0.5,    3L,
      "Sunitha",          0.5,    3L,
      "Pradeep",          0.5,    3L,
    "Madhumati",            0,    3L,
    "Prathibha",            0,    3L,
       "Nithin",            0,    4L,
      "Supriya",            0,    4L,
      "Sunitha",         0.25,    4L,
      "Pradeep",         0.25,    4L,
    "Madhumati",         0.25,    4L,
    "Prathibha",         0.25,    4L,
       "Nithin",          0.5,    5L,
      "Supriya",          0.5,    5L,
      "Sunitha",          0.5,    5L,
      "Pradeep",         0.75,    5L,
    "Madhumati",         0.75,    5L,
    "Prathibha",         0.75,    5L
  )

data2<-tibble::tribble(
         ~teacher_name, ~`0`, ~`0.25`, ~`0.5`, ~`0.75`,
              "Nithin",   0L,      3L,     5L,      6L,
             "Supriya",   0L,      4L,     6L,      4L,
             "Sunitha",   0L,      1L,     1L,      1L,
             "Pradeep",   0L,      4L,     5L,      2L,
           "Madhumati",   0L,      5L,     6L,      4L,
           "Prathibha",   0L,      3L,     2L,      4L
         )

Is this what you want?

data <- tibble::tribble(
  ~teacher_name, ~tch_support, ~week,
  "Nithin",         0.25,    1L,
  "Supriya",          0.5,    1L,
  "Sunitha",         0.75,    1L,
  "Pradeep",          0.5,    1L,
  "Madhumati",          0.5,    1L,
  "Prathibha",          0.5,    1L,
  "Nithin",          0.5,    2L,
  "Supriya",         0.25,    2L,
  "Sunitha",         0.25,    2L,
  "Pradeep",         0.25,    2L,
  "Madhumati",         0.75,    2L,
  "Prathibha",         0.75,    2L,
  "Nithin",         0.75,    3L,
  "Supriya",          0.5,    3L,
  "Sunitha",          0.5,    3L,
  "Pradeep",          0.5,    3L,
  "Madhumati",            0,    3L,
  "Prathibha",            0,    3L,
  "Nithin",            0,    4L,
  "Supriya",            0,    4L,
  "Sunitha",         0.25,    4L,
  "Pradeep",         0.25,    4L,
  "Madhumati",         0.25,    4L,
  "Prathibha",         0.25,    4L,
  "Nithin",          0.5,    5L,
  "Supriya",          0.5,    5L,
  "Sunitha",          0.5,    5L,
  "Pradeep",         0.75,    5L,
  "Madhumati",         0.75,    5L,
  "Prathibha",         0.75,    5L
)
library(dplyr)
library(tidyr)

data_summary <- data |> count(teacher_name, tch_support)
data_summary
#> # A tibble: 21 × 3
#>    teacher_name tch_support     n
#>    <chr>              <dbl> <int>
#>  1 Madhumati           0        1
#>  2 Madhumati           0.25     1
#>  3 Madhumati           0.5      1
#>  4 Madhumati           0.75     2
#>  5 Nithin              0        1
#>  6 Nithin              0.25     1
#>  7 Nithin              0.5      2
#>  8 Nithin              0.75     1
#>  9 Pradeep             0.25     2
#> 10 Pradeep             0.5      2
#> # … with 11 more rows
Summary_wide <- data_summary |> pivot_wider(names_from = tch_support, values_from = "n")
Summary_wide
#> # A tibble: 6 × 5
#>   teacher_name   `0` `0.25` `0.5` `0.75`
#>   <chr>        <int>  <int> <int>  <int>
#> 1 Madhumati        1      1     1      2
#> 2 Nithin           1      1     2      1
#> 3 Pradeep         NA      2     2      1
#> 4 Prathibha        1      1     1      2
#> 5 Sunitha         NA      2     2      1
#> 6 Supriya          1      1     3     NA

Created on 2023-04-14 with reprex v2.0.2

Yes this is how I want it to be. Thanks a lot for the help.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.