pivot_longer with multiple classes causes error (“No common type”)

前端 未结 3 1587
春和景丽
春和景丽 2021-01-01 21:22

I am running pivot_longer on multiple columns (i.e. two character columns and one numeric). I am encountering an error related to the class mismatch.

I

相关标签:
3条回答
  • 2021-01-01 22:09

    We can specify the values_ptype in this case (as the value columns differ in types)

    library(ggplot2)
    library(tidyr)
    library(dplyr)
    small_diamonds %>%  
       pivot_longer( - row_num, 
                 names_to = "key",
                 values_to = "val", values_ptypes = list(val = 'character'))
    # A tibble: 161,820 x 3
    #   row_num key   val    
    #     <int> <chr> <chr>  
    # 1       1 cut   Ideal  
    # 2       1 color E      
    # 3       1 price 326    
    # 4       2 cut   Premium
    # 5       2 color E      
    # 6       2 price 326    
    # 7       3 cut   Good   
    # 8       3 color E      
    # 9       3 price 327    
    #10       4 cut   Premium
    # … with 161,810 more rows
    
    0 讨论(0)
  • 2021-01-01 22:10

    The error is now there again in a different guise when the values_ptypes argument is used.

    library(tidyverse)
    
    small_diamonds <- diamonds %>% 
      select(cut, color, price) %>% 
      mutate(row_num = row_number())
    
    small_diamonds %>%  
      pivot_longer( - row_num, 
                    names_to = "key",
                    values_to = "val", 
                    values_ptypes = list(val = 'character'))
    #> Error: Can't convert <integer> to <character>.
    

    Therefore I need to use the values_transform argument to get the desired result.

    library(tidyverse)
    
      small_diamonds <- diamonds %>% 
        select(cut, color, price) %>% 
        mutate(row_num = row_number())
      
      small_diamonds %>%  
        pivot_longer( - row_num, 
                      names_to = "key",
                      values_to = "val", 
                      values_transform = list(val = as.character))
    #> # A tibble: 161,820 x 3
    #>    row_num key   val    
    #>      <int> <chr> <chr>  
    #>  1       1 cut   Ideal  
    #>  2       1 color E      
    #>  3       1 price 326    
    #>  4       2 cut   Premium
    #>  5       2 color E      
    #>  6       2 price 326    
    #>  7       3 cut   Good   
    #>  8       3 color E      
    #>  9       3 price 327    
    #> 10       4 cut   Premium
    #> # ... with 161,810 more rows
    

    Created on 2020-08-25 by the reprex package (v0.3.0)

    0 讨论(0)
  • 2021-01-01 22:24

    Using your example, you can see with str() that you have two vectors encoded as factors, and two as integers. pivot_longer demands that all vectors are of the same type, and throws the error you have reported.

        library(tidyverse)
        small_diamonds <- diamonds %>%
          select(cut, color, price) %>%
          mutate(row_num = row_number())
    
        str(small_diamonds)
    

    One solution is to convert all vector to characters with mutate.if, and then pass the pivot_longer command.

        small_diamonds %>% 
          mutate_if(is.numeric,as.character, is.factor, as.character) %>% 
          pivot_longer( - row_num, 
                names_to = "key",
                values_to = "val") 
    
    0 讨论(0)
提交回复
热议问题