Home > Blockchain >  pivot_longer: values_ptypes: can't convert <integer> to <character>
pivot_longer: values_ptypes: can't convert <integer> to <character>

Time:11-01

This question is related to this question dplyr median by group

and has already answers here: pivot_longer with multiple classes causes error ("No common type")

Now I have learned that we should use values_transform instead of values_ptypes.

I want to know what is the reason for this? Is there a deeper reason why values_ptypes does not work and values_transform work now:

Here is the above mentioned example that led me to this problem:

dataframe:

df1 <- data.frame(
  Type1 = c("A","A","A", "AB", "AB"),
  Type2 = c(1L,2L,2L, 1L, 1L),
  Value = c(1L, 2L, 1L, NA, NA), 
  Median = c(1L, 1.5, 1.5, NA, NA))

  Type1 Type2 Value Median
1     A     1     1    1.0
2     A     2     2    1.5
3     A     2     1    1.5
4    AB     1    NA     NA
5    AB     1    NA     NA

I would like to pivot_longer using values_ptypes argument like this: not working!

library(dplyr)
library(tidyr)
 df1 %>% 
   pivot_longer(
     cols = contains("Type"),
     names_to = "key",
     values_to = "val", 
     values_ptypes = list(val = 'character')
     )
Error: Can't convert <integer> to <character>.
Run `rlang::last_error()` to see where the error occurred.

This should make it possible to combine Type1 and Type2 but it is not working.

I want to know why in this case I can't force type conversion.

CodePudding user response:

pivot_longer needs the columns to be reshaped to have the same type. Here 'Type1' and 'Type2' are different in class. Change it to a single class by converting to character in values_transform. According to ?pivot_longer

names_ptypes, values_ptypes - A list of column name-prototype pairs. A prototype (or ptype for short) is a zero-length vector (like integer() or numeric()) that defines the type, class, and attributes of a vector. Use these arguments if you want to confirm that the created columns are the types that you expect. Note that if you want to change (instead of confirm) the types of specific columns, you should use names_transform or values_transform instead.

library(dplyr)
library(tidyr)
df1 %>% 
   pivot_longer(
     cols = contains("Type"),
     names_to = "key",
     values_to = "val", 
     values_transform = list(val = as.character))

-output

# A tibble: 10 × 4
   Value Median key   val  
   <int>  <dbl> <chr> <chr>
 1     1    1   Type1 A    
 2     1    1   Type2 1    
 3     2    1.5 Type1 A    
 4     2    1.5 Type2 2    
 5     1    1.5 Type1 A    
 6     1    1.5 Type2 2    
 7    NA   NA   Type1 AB   
 8    NA   NA   Type2 1    
 9    NA   NA   Type1 AB   
10    NA   NA   Type2 1    

pivot_longer calls pivot_longer_spec and within the function the line below generates the error

Browse[2]> 
debug: out <- vec_c(!!!val_cols, .ptype = val_type)
Browse[2]> 
Error: Can't convert <integer> to <character>.
Run `rlang::last_error()` to see where the error occurred.
  • Related