3

I have the following data frame (tibble):

library(tidyverse)
lines<-"
A,foo,9394981
B,bar,6826405
C,qux,1074885
D,gop,1493691   
A,foo,100
B,bar,200
C,qux,300
D,gop,400 
"
con <- textConnection(lines)
dat <- read.csv(con,header=FALSE)
close(con)
dat <- as.tibble(dat)
dat

Which looks like this:

# A tibble: 8 × 3

      V1     V2      V3
  <fctr> <fctr>   <dbl>
1      A    foo 9394981
2      B    bar 6826405
3      C    qux 1074885
4      D    gop 1493691
5      A    foo     100
6      B    bar     200
7      C    qux     300
8      D    gop     400

How can I covert it to:

foo        bar    qux     gop
9394981 6826405 1074885 1493691 
100     200      300      400
neversaint
  • 60,904
  • 137
  • 310
  • 477

2 Answers2

11

We can use spread from tidyr after creating a row index to take care of duplicate elements

library(tidyr)
library(dplyr)
dat %>% 
   select(-V1) %>% 
   group_by(V2) %>%
   dplyr::mutate(i1 = row_number()) %>% 
   spread(V2, V3) %>%
   select(-i1)

Or using dcast from data.table

library(data.table)
dcast(setDT(dat), rowid(V2) ~ V2, value.var = "V3")[, V2 := NULL][]
akrun
  • 874,273
  • 37
  • 540
  • 662
6

There's always unstack in base R

unstack(form=V3 ~ V2, x=dat)

      bar     foo     gop     qux
1 6826405 9394981 1493691 1074885
2     200     100     400     300
lmo
  • 37,904
  • 9
  • 56
  • 69