Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R, how can I change the location of row and column?

Tags:

r

dplyr

tidyr

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?

like image 219
user12388610 Avatar asked Nov 29 '22 07:11

user12388610


2 Answers

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)
like image 83
akrun Avatar answered Dec 04 '22 13:12

akrun


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
like image 36
tmfmnk Avatar answered Dec 04 '22 11:12

tmfmnk