Search code examples
rfor-loopseurat

PercentageFeatureSet() on several seurat objects


I have many seurat objects created from count matrixes downloaded from GEO. I want to use the PercentageFeatureSet() function on each of them to calculate the %MT.

I tried :

for (i in seu.list) {
  get(i)[["percent.mt"]] <- PercentageFeatureSet(get(i), pattern = "^MT-") 

but got the following error: Error in get(i) <- vtmp : could not find function "get<-"

How can I use PercentageFeatureSet() in loop?


Solution

  • It's probably better to do this using lapply. Try this:

    seu.list <- lapply(seu.list, function(seuset) {
      seuset[["percent.mt"]] <- PercentageFeatureSet(seuset, pattern = "^MT-")
      seuset
    })
    

    Alternatively, if you really want to use a for loop:

    for (i in seq_len(length(seu.list))) {
      seu.list[[i]][["percent.mt"]] <- PercentageFeatureSet(seu.list[[i]], pattern = "^MT-") 
    }