I have this raw dataset and the below is sample dataset :
X1 X2
1 Born 1946-05-27
2 bioguide A000370
3 Born 1979-06-19
4 bioguide A000371
5 Born 1980-04-18
6 bioguide A000367
7 Born 1958-06-12
8 bioguide A000369
9 Born 1948-03-23
10 bioguide B001291
Using this, my desired output is below:
Born biouguide
1 1946-05-27 A000370
2 1979-06-19 A000371
3 1980-04-18 A000367
4 1958-06-12 A000369
5 1980-04-18 A000367
Also, the below is dput of raw dataset:
structure(list(X1 = c("Born", "bioguide", "Born", "bioguide",
"Born", "bioguide", "Born", "bioguide", "Born", "bioguide"),
X2 = c("1946-05-27", "A000370", "1979-06-19", "A000371",
"1980-04-18", "A000367", "1958-06-12", "A000369", "1948-03-23",
"B001291")), row.names = c(NA, 10L), class = "data.frame")
Could you please help me make the desired output?
We can use pivot_wider
library(dplyr)
library(tidyr)
df1 %>%
group_by(X1) %>%
mutate(rn = row_number()) %>%
pivot_wider(names_from = X1, values_from = X2) %>%
select(-rn)
# A tibble: 5 x 2
# Born bioguide
# <chr> <chr>
#1 1946-05-27 A000370
#2 1979-06-19 A000371
#3 1980-04-18 A000367
#4 1958-06-12 A000369
#5 1948-03-23 B001291
Or in base R
unstack(df1, X2 ~ X1)
One base R
option could be:
data.frame(Born = df[c(TRUE, FALSE), 2],
biouguide = df[c(FALSE, TRUE), 2])
Born biouguide
1 1946-05-27 A000370
2 1979-06-19 A000371
3 1980-04-18 A000367
4 1958-06-12 A000369
5 1948-03-23 B001291
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With