I am trying to gather untidy data from wide to long format. I have 748 variables, that need to be condensed to approximately 30.
In this post, I asked: how to tidy my wide data? The answer: use gather().
However, I am still struggling to gather multiple columns and was hoping you could pinpoint where I'm going wrong.
Reproducible example:
tb1 <- tribble(~x1,~x2,~x3,~y1,~y2,~y3,
1,NA,NA,NA,1,NA,
NA,1,NA,NA,NA,1,
NA,NA,1,NA,NA,1)
# A tibble: 3 x 6
# x1 x2 x3 y1 y2 y3
# <dbl> <dbl> <dbl> <lgl> <dbl> <dbl>
#1 1 NA NA NA 1 NA
#2 NA 1 NA NA NA 1
#3 NA NA 1 NA NA 1
with x1-y3 having the following characteristics:
1 x1 Green
2 x2 Yellow
3 x3 Orange
4 y1 Yes
5 y2 No
6 y3 Maybe
I tried this:
tb1 %>%
rename("Green" =x1,
"Yellow"=x2,
"Orange"=x3,
"Yes"=y1,
"No"=y2,
"Maybe"=y3) %>%
gather(X,val,-Green,-Yellow,-Orange) %>%
gather(Y,val,-X) %>%
select(-val)
I did get an output that I wanted for these variables, but I can't imagine how to do this for 700+ variables?! Is there a more effective way?
tb1 %>%
rename("Green" =x1,
"Yellow"=x2,
"Orange"=x3,
"Yes"=y1,
"No"=y2,
"Maybe"=y3) %>%
gather(X,val,-Green,-Yellow,-Orange) %>%
filter(!is.na(val)) %>%
select(-val) %>%
gather(Y,val,-X) %>%
filter(!is.na(val)) %>%
select(-val)
# A tibble: 3 x 2
X Y
<chr> <chr>
1 No Green
2 Maybe Yellow
3 Maybe Orange
I think I might be just not acquainted enough with gather() so this is probably a stupid question - would appreciate the help. Thanks!