Home > Net >  How can i pivot longer in R using dplyr with logical and character columns?
How can i pivot longer in R using dplyr with logical and character columns?

Time:06-09

I have a dataframe that looks like this :

id month C1 C2 C3
A 1 TRUE FALSE ok
A 2 FALSE FALSE not
A 3 FALSE TRUE warning
B 1 TRUE TRUE not
B 2 FALSE FALSE not
B 3 TRUE FALSE ok

And i want to pivot longer from wide in R in order to look like this :

id month Check category result
A 1 C1 TRUE
A 2 C1 FALSE
A 3 C1 FALSE
A 1 C2 FALSE
A 2 C2 FALSE
A 3 C2 TRUE
A 1 C3 ok
A 2 C3 not
A 3 C3 warning
B 1 C1 TRUE
B 2 C1 FALSE
B 3 C1 TRUE
B 1 C2 TRUE
B 2 C2 FALSE
B 3 C2 FALSE
B 1 C3 not
B 2 C3 not
B 3 C3 OK

My effort in R is the following:



id = c(rep("A",3),rep("B",3));id
month=c(1,2,3,1,2,3)
C1 = c(T,F,F,T,F,T);C1
C2 = c(F,F,T,T,F,F);C1
C3 = c("ok","not","wanrning","not","not","ok")
dat = tibble(id,month,C1,C2,C3);dat
dat%>%
  tidyr::pivot_longer(!c(id,month), names_to = "check_spot", values_to = c("control_check"))

But R reports me an error saying :

Error: Can't combine `C1` <logical> and `C3` <character>.

How can i fix this and take as an outpt the wanted table ? Any help ?

CodePudding user response:

Just change logical to character will helps.

dat%>%
  mutate(across(is.logical, ~as.character(.x))) %>%
  tidyr::pivot_longer(!c(id,month), names_to = "check_spot", values_to = c("control_check"))

   id    month check_spot control_check
   <chr> <dbl> <chr>      <chr>        
 1 A         1 C1         TRUE         
 2 A         1 C2         FALSE        
 3 A         1 C3         ok           
 4 A         2 C1         FALSE        
 5 A         2 C2         FALSE        
 6 A         2 C3         not          
 7 A         3 C1         FALSE        
 8 A         3 C2         TRUE         
 9 A         3 C3         wanrning     
10 B         1 C1         TRUE         
11 B         1 C2         TRUE         
12 B         1 C3         not          
13 B         2 C1         FALSE        
14 B         2 C2         FALSE        
15 B         2 C3         not          
16 B         3 C1         TRUE         
17 B         3 C2         FALSE        
18 B         3 C3         ok    

reorder?

dat%>%
  mutate(across(is.logical, ~as.character(.x))) %>%
  tidyr::pivot_longer(!c(id,month), names_to = "check_spot", values_to = c("control_check")) %>%
  arrange(id, check_spot)

   id    month check_spot control_check
   <chr> <dbl> <chr>      <chr>        
 1 A         1 C1         TRUE         
 2 A         2 C1         FALSE        
 3 A         3 C1         FALSE        
 4 A         1 C2         FALSE        
 5 A         2 C2         FALSE        
 6 A         3 C2         TRUE         
 7 A         1 C3         ok           
 8 A         2 C3         not          
 9 A         3 C3         wanrning     
10 B         1 C1         TRUE         
11 B         2 C1         FALSE        
12 B         3 C1         TRUE         
13 B         1 C2         TRUE         
14 B         2 C2         FALSE        
15 B         3 C2         FALSE        
16 B         1 C3         not          
17 B         2 C3         not          
18 B         3 C3         ok     

CodePudding user response:

 dat %>%
    pivot_longer(-c(id,month), names_to = "check_spot", values_to = "control_check",
                values_transform = as.character)  %>%
   arrange(id, check_spot)

# A tibble: 18 x 4
   id    month check_spot control_check
   <chr> <dbl> <chr>      <chr>        
 1 A         1 C1         TRUE         
 2 A         2 C1         FALSE        
 3 A         3 C1         FALSE        
 4 A         1 C2         FALSE        
 5 A         2 C2         FALSE        
 6 A         3 C2         TRUE         
 7 A         1 C3         ok           
 8 A         2 C3         not          
 9 A         3 C3         wanrning     
10 B         1 C1         TRUE         
11 B         2 C1         FALSE        
12 B         3 C1         TRUE         
13 B         1 C2         TRUE         
14 B         2 C2         FALSE        
15 B         3 C2         FALSE        
16 B         1 C3         not          
17 B         2 C3         not          
18 B         3 C3         ok      
  • Related