具有多个类的pivot_longer导致错误(“无通用类型”)

时间:2019-09-26 20:45:03

标签: r tidyr

我在多列(即两个字符列和一个数字)上运行pivot_longer。我遇到与类不匹配有关的错误。

我已经调查了有关“强制”选项的文档,但在pivot_longer中没有看到任何参数来指定要使用的类-或允许该功能自动检测最通用的类​​。

pivot_longer中是否有任何参数可以避免此错误?还是需要在运行pivot_longer之前将列转换为单个类?

library(dplyr)
library(tidyr)
library(ggplot2) # Just for `diamonds` dataset

small_diamonds <- diamonds %>% 
  # Select a few columns (two character, one numeric, specifically integers)
  select(cut, color, price) %>% 
  # Create a row_id
  mutate(row_num = row_number()) 

# This works with `gather`
small_diamonds %>% 
  gather(key, val, - row_num)

# This fails due to class error:
small_diamonds %>% 
  # Pivot data
  pivot_longer( - row_num, 
                names_to = "key",
                values_to = "val")

# Output
# Error: No common type for `cut` <ordered<4bd7e>> and `price` <integer>.
# Call `rlang::last_error()` to see a backtrace

# Convert columns to a single class (character) and then use `pivot_longer`. 
# Runs successfully
small_diamonds %>% 
  mutate_all(as.character) %>% 
  # Pivot data
  pivot_longer( - row_num, 
                names_to = "key",
                values_to = "val")

3 个答案:

答案 0 :(得分:5)

在这种情况下,我们可以指定values_ptype(因为值列的类型不同)

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

答案 1 :(得分:5)

当使用values_ptypes参数时,错误现在又以另一种形式出现。

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>.

因此,我需要使用values_transform参数来获得所需的结果。

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

reprex package(v0.3.0)于2020-08-25创建

答案 2 :(得分:4)

在您的示例中,通过str()可以看到,您有两个向量编码为因子,两个向量为整数。 ivot_longer要求所有向量都属于同一类型,并引发您报告的错误。

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

    str(small_diamonds)

一种解决方案是使用mutate.if将所有矢量转换为字符,然后传递ivot_longer命令。

    small_diamonds %>% 
      mutate_if(is.numeric,as.character, is.factor, as.character) %>% 
      pivot_longer( - row_num, 
            names_to = "key",
            values_to = "val")