简体   繁体   中英

How to create a column by rownames in R

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM