how to de-aggregate a table in R
3
1
Entering edit mode
5.5 years ago
lessismore ★ 1.3k

Dear all,

i have this table. does someone know how to de-aggregate it ?

plant   tissue  count
tomato  leaf    1
tomato  root    4
tomato  shoot   5
solanus leaf    3
solanus root    2
solanus shoot   4

what i want is a dataframe like that:

    leaf    root    shoot
tomato  1   4   5
solanus 3   2   4

thanks in advance for you tips

r • 1.7k views
ADD COMMENT
1
Entering edit mode

See below SO post, there are many alternatives, this is called "convert from long-to-wide format":

ADD REPLY
0
Entering edit mode

I add another option to solve this with tidyr:

1. reproduce the same table

test <- structure(list(plant = c("tomato", "tomato", "tomato", "solanus", 
    "solanus", "solanus"), tissue = c("leaf", "root", "shoot", "leaf", 
    "root", "shoot"), count = c(1L, 4L, 5L, 3L, 2L, 4L)), class = "data.frame", row.names = c(NA, 
    -6L))

2. make it wider

library(tidyverse)
test %>% pivot_wider(names_from = tissue, values_from = count)
ADD REPLY
3
Entering edit mode
5.5 years ago

Buenas tardes amiga/o,

You can use dcast():

df
    plant tissue count
1  tomato   leaf     1
2  tomato   root     4
3  tomato  shoot     5
4 solanus   leaf     3
5 solanus   root     2
6 solanus  shoot     4

require(data.table)

dcast(data = df, formula = plant ~ tissue, value.var = 'count')
    plant leaf root shoot
1 solanus    3    2     4
2  tomato    1    4     5

Kevin

ADD COMMENT
1
Entering edit mode
5.5 years ago

Base R:

> xtabs(count ~ ., data = test)
         tissue
plant     leaf root shoot
  solanus    3    2     4
  tomato     1    4     5

> test
    plant tissue count
1  tomato   leaf     1
2  tomato   root     4
3  tomato  shoot     5
4 solanus   leaf     3
5 solanus   root     2
6 solanus  shoot     4

out of R, with datamash:

$ datamash  -sH crosstab 1,2 unique 3  <test.txt 

GroupBy(plant)  GroupBy(tissue) unique(count)
    leaf    root    shoot
solanus 3   2   4
tomato  1   4   5
ADD COMMENT
0
Entering edit mode
5.5 years ago
Chirag Parsania ★ 2.0k

Tidy way

library(tidyverse)
data <- tibble(plant = c("tomato","tomato","tomato","solanus" ,"solanus","solanus") , tissue = c("leaf" ,"root","shoot","leaf","root","shoot") , count = c(1,4,5,3,2,4))

> data
# A tibble: 6 x 3
  plant   tissue count
  <chr>   <chr>  <dbl>
1 tomato  leaf       1
2 tomato  root       4
3 tomato  shoot      5
4 solanus leaf       3
5 solanus root       2
6 solanus shoot      4

data %>% spread(key = tissue ,  value = count) 

# A tibble: 2 x 4
  plant    leaf  root shoot
  <chr>   <dbl> <dbl> <dbl>
1 solanus     3     2     4
2 tomato      1     4     5
ADD COMMENT

Login before adding your answer.

Traffic: 1682 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6