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