Skip to contents

Function to transforms common abundance table formats into a "long" format.

Usage

prepare_tidy_data(data, sample_names, samples_in = "cols", ...)

Arguments

data

a data.frame in "wide" format, with samples in either columns or rows. This data.frame should not include any data besides abundance values per sample, per taxonomic unit. Additional data (e.g. taxonomy details) should be added afterwards.

sample_names

a vector with the name of all samples.

samples_in

a vector specifying the location of the samples. It can either be "cols" (default) if samples are in columns, or "rows" if samples are in rows.

...

additional arguments

Value

An abundance table in long format, compatible with dplyr pipes and ulrb package functions.

Details

This function guarantees that the abundance table includes one column with sample ID's and one column with abundance.

Common species table formats

There are two common formats for abundance tables:

  • samples as rows and taxa as columns;

  • taxa as rows and samples as columns.

However, both formats are not tidy/long, because they include several columns with the same variable. They are in a "wide format" instead of a "long format".

This function re-organizes samples and taxa so that there is a single column with the samples ID's and another with the abundance scores. Extra columns are allowed.

See also

Examples

library(dplyr)
#
sample_names <- c("ERR2044662", "ERR2044663", "ERR2044664",
                   "ERR2044665", "ERR2044666", "ERR2044667",
                   "ERR2044668", "ERR2044669", "ERR2044670")

# Example for samples in cols and with additional data available
prepare_tidy_data(nice, sample_names = sample_names, samples_in = "cols")
#> # A tibble: 4,716 × 10
#>    OTU   Domain      Phylum    Class Order Family Genus Species Sample Abundance
#>    <chr> <chr>       <chr>     <chr> <chr> <chr>  <chr> <chr>   <chr>      <int>
#>  1 OTU_2 sk__Archaea p__Eurya… c__C… NA    NA     NA    NA      ERR20…       165
#>  2 OTU_2 sk__Archaea p__Eurya… c__C… NA    NA     NA    NA      ERR20…       323
#>  3 OTU_2 sk__Archaea p__Eurya… c__C… NA    NA     NA    NA      ERR20…        51
#>  4 OTU_2 sk__Archaea p__Eurya… c__C… NA    NA     NA    NA      ERR20…        70
#>  5 OTU_2 sk__Archaea p__Eurya… c__C… NA    NA     NA    NA      ERR20…       134
#>  6 OTU_2 sk__Archaea p__Eurya… c__C… NA    NA     NA    NA      ERR20…       216
#>  7 OTU_2 sk__Archaea p__Eurya… c__C… NA    NA     NA    NA      ERR20…         0
#>  8 OTU_2 sk__Archaea p__Eurya… c__C… NA    NA     NA    NA      ERR20…        11
#>  9 OTU_2 sk__Archaea p__Eurya… c__C… NA    NA     NA    NA      ERR20…         0
#> 10 OTU_3 sk__Archaea p__Eurya… c__C… o__C… f__    g__   s__Mar… ERR20…         0
#> # ℹ 4,706 more rows

# Example for samples in rows
# Select columns with samples from nice
nice_rows <- nice %>% select(all_of(sample_names))

# Change columns to rows
nice_rows <- nice_rows %>% t() %>% as.data.frame()

# Turn colnames into phylogenetic units ID
colnames(nice_rows) <- paste0("OTU_", seq_along(colnames(nice_rows)))

prepare_tidy_data(nice_rows, sample_names = sample_names, samples_in = "rows")
#> Taxa_id assumes each column is a taxonomic unit.
#> # A tibble: 4,716 × 3
#> # Groups:   Sample [9]
#>    Sample     Abundance Taxa_id
#>    <chr>          <int> <chr>  
#>  1 ERR2044662       165 OTU_1  
#>  2 ERR2044663       323 OTU_1  
#>  3 ERR2044664        51 OTU_1  
#>  4 ERR2044665        70 OTU_1  
#>  5 ERR2044666       134 OTU_1  
#>  6 ERR2044667       216 OTU_1  
#>  7 ERR2044668         0 OTU_1  
#>  8 ERR2044669        11 OTU_1  
#>  9 ERR2044670         0 OTU_1  
#> 10 ERR2044662         0 OTU_2  
#> # ℹ 4,706 more rows