Émilie Tremblay Émilie Tremblay - 3 months ago 7
R Question

R asks for a list which seems to be a list according to is.list (=TRUE)

I am using the RAM package.
The function I use is very simple for diversity index, adding up a column in my metadata ;

outname <-OTU.diversity(data=OTUtables, meta=metatables)



(Arguments: data a list of OTU tables.

meta the metadata to append the outputs)


I am looping it but I get this error:


please provide otu tables as list; see ?RAM.input.formatting


So I go to that help menu and read this:


one data set:

data=list(data=otu)


multiple data sets:

data=list(data1=otu1, data2=otu2, data3=otu3)



here is my code:

i <- 1

for(i in 1:nrow(metadataMasterTax)){
temp <- read.table(paste(metadataMasterTax$DataAnFilePath[i], metadataMasterTax$meta[i], sep = ""),
sep = "\t", header = TRUE, dec = ".", comment.char = "", quote = "", stringsAsFactors = TRUE,
as.is = TRUE)
temp2 <- temp
temp2$row.names <- NULL #to unactivate numbers generated in the margin

trans <- read.table(paste(metadataMasterTax$taxPath[i], metadataMasterTax$taxName[i], sep = ""),
sep = "\t", header = TRUE, dec = ".", comment.char = "", quote = "", stringsAsFactors = TRUE,
as.is = TRUE, check.names = FALSE)
trans2 <- trans
trans2$row.names <- NULL #to unactivate numbers generated in the margin

data=list(data=trans2[i])

temp2[i] <- OTU.diversity(data=trans2[i], meta=temp2[i])

# Error in OTU.diversity(trans2, temp2) :
# please provide otu tables as list; see ?RAM.input.formatting
# is.list(trans2)
# [1] TRUE
# is.list(data)
# [1] TRUE

temp$taxonomy <- temp2$taxonomy

write.table(temp, file=paste(pathDataAn, "diversityDir/", metadataMasterTax$ShortName[i], ".meta.div.tsv", sep = ""),
append = FALSE,
sep = "\t",
row.names = FALSE)
}


Can anyone help me please....
thanks a lot

Answer

Because the main problem appears to be getting the OTU.diversity function to work, I focus on this issue. The code snippet below runs OTU.diversity without any problems, using the Google sheets data provided by OP.

library(gsheet)
library(RAM)

for (i in 1:2) {
  # Meta data
  temp <- as.data.frame(gsheet2tbl("https://drive.google.com/open?id=1hF47MbYZ1MG6RzGW-fF6tbMT3z4AxbGN5sAOxL4E8xM"))
  temp$row.names <- NULL

  # OTU
  trans <- as.data.frame(gsheet2tbl("https://drive.google.com/open?id=1gOaEjDcs58T8v1GA-OKhnUsyRDU8Jxt2lQZuPWo6XWU"))
  trans$row.names <- NULL

  rownames(temp) <- colnames(trans)[-ncol(trans)]

  temp2 <- OTU.diversity(data = list(data = trans), meta = temp)

  write.table(temp2, 
              file      = paste0("file", i, ".meta.div.tsv"), # replace 
              append    = FALSE,
              sep       = "\t",
              row.names = FALSE)
}

Replace for (i in 1:2) with for(i in 1:nrow(metadataMasterTax)), as.data.frame(gsheet2tbl(...)) with read.table(...), and the file argument in write.table with the appropriate string.