I have a data.frame
that contains duplicate column names that I want to lengthen. I don't want to fix the names because they correspond to values in my future column. I am trying to use pivot_longer
but it throws an error.
Error: Can't transform a data frame with duplicate names.
I looked at the documentation for the function and used the "names_repair" argument to get around the issue but it didn't help.
I also found this issue on tidyvere's github but I'm not sure what's going on in there.
Here's my code:
library(dplyr)
library(tidyr)
df %>%
mutate_all(as.character) %>%
pivot_longer(-a, names_to = "Names", values_to = "Values", names_repair = "minimal")
Is there a way to do this?
Desired output:
a Names Values
<chr> <chr> <chr>
1 1 b 4
2 1 c a
3 1 c d
4 2 b 5
5 2 c b
6 2 c e
7 3 b 6
8 3 c c
9 3 c f
Sample data:
df <- setNames(data.frame(c(1,2,3),
c(4,5,6),
c("a","b","c"),
c("d","e","f"),
stringsAsFactors = F),
c("a","b","c","c"))