I'm working to get a better handle on pivot_longer, coming from a gather user. From the source documents it seems like I should be able to do the following in a single command using name_pattern or names_sep but I've been unable to find a working solution.
Data
id1 <- c("person1","person2","person3")
id2 <- c("1001","1002","1003")
id3 <- c("2001","2002", "2003")
value_1 <- c(10,50,100)
value_2 <- c(20,200, 2000)
status_1 <- c("OK","BAD","GOOD")
status_2 <- c("AWFUL","EXCELLENT","AVERAGE")
df <- data.frame(id1,id2,id3,value_1,value_2,status_1,status_2)
Expected output:
id1 id2 id3 gradeLevel status value
1 person1 1001 2001 1 OK 10
2 person1 1001 2001 2 AWFUL 20
3 person2 1002 2002 1 BAD 50
4 person2 1002 2002 2 EXCELLENT 200
5 person3 1003 2003 1 GOOD 100
6 person3 1003 2003 2 AVERAGE 2000
I can achieve this with a gather statement and a few extra lines:
df %>%
gather(key, value,-id1, -id2,-id3) %>%
separate(key, c('cat', 'gradeLevel'),sep ="_") %>%
distinct() %>%
spread(cat,value)
Is there a way to simplify this with pivot_longer? I think names_pattern is promising but I struggle with regex. Most of my attempts are attempting to combine different types of columns (double & factor)
df %>%
pivot_longer(cols = value_1:status_2, names_to = c('col', '.value'), names_pattern = "(.*)_(.)")