Created
May 11, 2021 19:40
-
-
Save aurielfournier/abe96ddb8bd7bb6735c1d294f13a32ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyr) | |
long <- data.frame(Lname = c("A","B","C"), | |
c = c("c1","c2","c2"), | |
country=c("NZ","NZ","AL")) | |
> pivot_wider(data=long, values_from="Lname",names_from="country") | |
# A tibble: 2 x 3 | |
c NZ AL | |
<chr> <chr> <chr> | |
1 c1 A NA | |
2 c2 B C |
I think this may give you what you want. If the c is not important, it can be removed from group_by().
long2 = long %>%
filter(country != "NA") %>%
group_by(country, c) %>%
summarize(Lname=toString(Lname)) %>%
pivot_wider(values_from="Lname",names_from="country")
ooh that is so close, but it ends up with multiple Lnames in a single cell for some reason?
output (without the c) is:
long2
A tibble: 1 x 2
AL NZ
1 C, B A, B
Change relevant bit to:
toString(unique(Lname))
hmm, I get same output with that?
but I can deal with this, thanks so much all!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool, thanks so much!