Data/matrix manipulation in R

50 views Asked by At

Dear stackoverflow community,

I think im experienced with R, but last week R proved me wrong. I think my problem is very easy to solve, but i can't, so im reaching out for you guys!

I got a problem with my some sequencing data, and want it to be in another form of matrix (like an OTU table). It's a very large dataset so the table listed below is a fraction of it. In total there are 8 barcodes, and like 14 different species.

it is now in the following matrix/structure:

  barcode    name         fraction
1 barcode01  Escherichia  0.2
2 barcode01  Bacteria     0.6
3 barcode02  Escherichia  0.2
4 barcode02  Bacteria     0.3
5 barcode03  Escherichia  0.4
6 barcode03  Bacteria     0.1

I want it to be in the following structure (like an OTU table):

            barcode01 barcode02 barcode03
Escherichia 0.2       0.2       0.4
Bacteria    0.6       0.3       0.1

I tried to write the data as vector:

asv <- as.vector(test)
matrix(asv, dimnames = list(asv$barcode, asv$name))

but I got an error message saying:

Error in matrix(asv, dimnames = list(asv$barcode, asv$name)) : 
  length of 'dimnames' [1] not equal to array extent

Does anyone have an answer/solution? Look out hearing from you!

Kind regards, Marwin

1

There are 1 answers

0
Friede On

I think you want

library(tidyr)
data |>
  pivot_wider(names_from = barcode, values_from = fraction)
#> # A tibble: 2 × 4
#>   name        barcode01 barcode02 barcode03
#>   <chr>           <dbl>     <dbl>     <dbl>
#> 1 Escherichia       0.2       0.2       0.4
#> 2 Bacteria          0.6       0.3       0.1

Created on 2023-11-08 with reprex v2.0.2

Maybe you need to coerce your matrix to class data frame in a previous step.

Data:

data <- read.table(text = "  barcode    name         fraction
1 barcode01  Escherichia  0.2
2 barcode01  Bacteria     0.6
3 barcode02  Escherichia  0.2
4 barcode02  Bacteria     0.3
5 barcode03  Escherichia  0.4
6 barcode03  Bacteria     0.1", header = TRUE)