Factors Levels to become columns name

Hello!
I have an issue

I have the first table and i want my data become the the second one

I have questions with 5 levels responses (5_Very satisfied 4_Satisfied 3_Partly satisfied 2_Dissatisfied 1_Very dissatisfied)
i want theses levels to become the name of the columns in my new data.
Sorry if im not clear! my english is really week.thanks!

Hi, in the future, please share a reproducible example. I was able to help today but share your data in a way that is easy for us to get in R. See some tips on how to do this here: FAQ: How to do a minimal reproducible example ( reprex ) for beginners

Now to answer your question. I would pivot the data twice, once to long and then back to wide.

library(tidyverse)

datinit <- tibble(
   Country=c("Austria", "Austria", "Belgium", "Denmark", "Denmark"),
   Dealername=str_c("Dealer", c(4, 7, 24, 87, 69)),
   Q1=c(rep("5_Very_satistified", 3), "3_Partly_satisfied", "1_Very_dissatistified"),
   Q2=c(rep("5_Very_satistified", 2), "4_Satisfied", "2_Dissatisfied", "4_Satisfied")
)

datinit %>%
   pivot_longer(cols=c(Q1, Q2), names_to="QUESTIONS") %>%
   mutate(val1=1) %>%
   pivot_wider(names_from="value", values_from=val1, values_fill=0)
#> # A tibble: 10 x 8
#>    Country Dealername QUESTIONS `5_Very_satistif~ `4_Satisfied` `3_Partly_satis~
#>    <chr>   <chr>      <chr>                 <dbl>         <dbl>            <dbl>
#>  1 Austria Dealer4    Q1                        1             0                0
#>  2 Austria Dealer4    Q2                        1             0                0
#>  3 Austria Dealer7    Q1                        1             0                0
#>  4 Austria Dealer7    Q2                        1             0                0
#>  5 Belgium Dealer24   Q1                        1             0                0
#>  6 Belgium Dealer24   Q2                        0             1                0
#>  7 Denmark Dealer87   Q1                        0             0                1
#>  8 Denmark Dealer87   Q2                        0             0                0
#>  9 Denmark Dealer69   Q1                        0             0                0
#> 10 Denmark Dealer69   Q2                        0             1                0
#> # ... with 2 more variables: 2_Dissatisfied <dbl>, 1_Very_dissatistified <dbl>

Created on 2021-12-15 by the reprex package (v2.0.1)

Thank you StatSteph!
next time i will try to more clear.You helped me a lot

This topic was automatically closed 21 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.