Search code examples
rtransformdonut-chart

How to combine two columns into one in R, so that each value in the second column becomes every other value in the first column?


I have the following dataset:

library(plyr)
library(dplyr)
detach(package:plyr)    
library(dplyr)

position <- c("A", "A", "B", "C", "D", "E", "F", "F")
level <- c("P", "S", "J", "J", "P", "S", "S", "S")
car <- c("yes", "no", "no", "no", "no", "yes", "no", "yes")
car.data <- data.frame(position, level, car)

percent_car <- car.data %>%
  group_by(position) %>%
  summarise (car_yes = sum(car == "yes"), car_no = sum(car == "no")) %>%
  mutate(yes_perc = car_yes/(car_yes + car_no), no_perc = car_no/(car_yes + car_no))
percent_car

with the output being:

    position car_yes car_no yes_perc no_perc
  <chr>      <int>  <int>    <dbl>   <dbl>
1 A              1      1      0.5     0.5
2 B              0      1      0       1  
3 C              0      1      0       1  
4 D              0      1      0       1  
5 E              1      0      1       0  
6 F              1      1      0.5     0.5

My goal ist to create donut charts from this dataset and for that I would need the data in a form in which each position has two rows- one for the people in the position who have a car and one for people, who doesn´t. Also I want to calculate the ymax and the ymin for each row:

 Position yes_no car_yes_no yes_no_perc  ymax  ymin
   <chr>  <chr>     <dbl>    <dbl>       <dbl> <dbl>
 1 A       yes         1       0.5        0.5   0    
 2 A       no          1       0.5        1     0.5    
 3 B       yes         0       0          0     0    
 4 B       no          1       1          1     0    
 5 C       yes         0       0          0     0    
 6 C       no          1       1          1     0    
 7 D       yes         0       0          0     0    
 8 D       no          1       1          1     0    
 9 E       yes         1       1          0     0    
10 E       no          0       0          1     0    
11 F       yes         1       0.5        0.5   0    
12 F       no          1       0.5        1     0.5  
   

Currently I am creating the second dataset in Excel. Do you know a way how I can directly create such a dataset in R?

Thanks!


Solution

  • Not fully sure what your logic is for the ymin/ymax but this is the general idea, run it by line to see what's happening.

    percent_car %>%
      pivot_longer(names_to = "key", values_to = "value", -position) %>%
      mutate(
        yes_no = str_extract(key, "yes|no"),
        key = str_remove_all(key, "yes|no|_")
      ) %>%
      pivot_wider(names_from = key, values_from = value) %>%
      arrange(position) %>%
      mutate(
        ymax = if_else(yes_no == "yes", perc, 1),
        ymin = if_else(yes_no == "yes", 0, 1-perc)
          
        )
      )
    

    case_when will be your friend if the if_else needs to be nested