pivot_longer error: impossible to combine different classes?

Viewed 7428

Let's say I have this df:

df <- data.frame(a= "a", b = 1, col3 = "c", col4 = "d") %>% print()

When I try to "lengthen" it using this code,

df %>% pivot_longer(1:4, names_to = "test", values_to = "test2")

I get the following error:

Error: Can't combine `a` <factor<127a2>> and `b` <double>.

Why do I get this? Because the first column and first row both have the same value, "a" (I've tried names_repair argument without success)? Or because a and b are different classes? What I don't understand is that I often pivot data of different classes and it always works. Or, is it because I am trying to pivot the actual header (this is what I need to do indeed)?

Any help much appreciated. Desired output:

#a     a
#b     1 
#col3  c
#col4  d
2 Answers

You're getting this message because a and b are different classes "see the error message". Define the new column class explicitly by using values_ptypes, this class must be lower than a and b classes in order to cast a and b to it.

tidyr::pivot_longer(df, 1:4, names_to = "test", values_to = "test2", 
                    values_ptypes = list(test2=character()))

# A tibble: 4 x 2
  test  test2
  <chr> <chr>
1 a     a    
2 b     1    
3 col3  c    
4 col4  d    

This might be more straightforward in base R, where you may simply transpose the data frame. Because it coerces into a matrix just convert back as.data.frame.

as.data.frame(t(df))
#      V1
# a     a
# b     1
# col3  c
# col4  d

Use unname to get cleaner row names.

as.data.frame(unname(t(df)))
#   V1
# 1  a
# 2  1
# 3  c
# 4  d

If you want the names as an extra variable, do this:

data.frame(V1=names(df), V2=unname(t(df)))
#     V1 V2
# 1    a  a
# 2    b  1
# 3 col3  c
# 4 col4  d
Related