Home > OS >  How to create a column by rownames in R
How to create a column by rownames in R

Time:06-02

I have a data like that

List item

Col1 Col2
A     1
A     2
A     3
B     1
B     2
B     3

I want to make it like that

A    B
1    1
2    2
3    3

How can I do this? Thanks

CodePudding user response:

Pivot_wider(df, names_from= col1, values_from = col2)

CodePudding user response:

A possible solution, based on tidyr::pivot_wider:

library(tidyverse)

pivot_wider(df, names_from = Col1, values_from = Col2, values_fn = list) %>% 
  unchop(everything())

#> # A tibble: 3 × 2
#>       A     B
#>   <int> <int>
#> 1     1     1
#> 2     2     2
#> 3     3     3
  • Related