Home > Net >  How to save multiple variables with 1 line of code in R?
How to save multiple variables with 1 line of code in R?

Time:08-08

I have 7 large seurat objects, saved as sn1, sn2, sn3 ... sn7 I am trying to do scaledata on all 7 samples. I could write the same line 7 times as:

all.genes <- rownames(sn1)
snN1<-ScaleData(sn1, features = all.genes)

all.genes <- rownames(sn2)
snN2<-ScaleData(sn2, features = all.genes)

all.genes <- rownames(sn2)
snN2<-ScaleData(sn2, features = all.genes)

. . .

This would work perfectly. Since I have to use all 7 samples for quite a while still, I thought I'd save time and make a for loop to do the job with one line of code, but I am unable to save the varables, getting an error "Error in { : target of assignment expands to non-language object".

This is what I tried:

samples<-c("sn1", "sn2", "sn3", "sn4", "sn5", "sn6", "sn7")
list<-c("snN1", "snN2", "snN3", "snN4", "snN5", "snN6", "snN7")

for (i in samples) {
all.genes <- rownames(get(i))
list[1:7]<-ScaleData(get(i), features = all.genes)
}

How do I have to format the code so it could create varables snN1, snN2, snN3 and save scaled data from sn1, sn2, sn3... to each respective new variable?

CodePudding user response:

I think the error is in this line: list[1:7]<-ScaleData(get(i), features = all.genes). You are saying to the for-loop to reference the output of the function ScaleData, to the 7 string variables in the list, which makes no sense. I think you are looking for the function assign(), but it is recommended to use it in very specific contexts.

Also, there're better methods that for-loops in R, for example apply() and related functions. I recommend to you to create as a custom function the steps you want to apply, and then call lapply() to iteratively - as a for-loop would do - change every variable and store it in a list. To call every 'snX' variable as the input you can reference them in a list that direct to them.

  # Custom function
    custom_scale <- function(x){
      all.genes <- rownames(x)
      y = ScaleData(x, features = all.genes)
    }
    
    # Apply custom function and return saved in a list
      # Create a list that directo to every variable
    samples = list(sn1, sn2, sn3, sn4, sn5, sn6, sn7) # Note I'm not using characters, I'm referencing the actual variable.
      # use lapply to iterate over the list and apply your custom function, saving the result as your list
    scaled_Data_list = lapply(samples, function(x) custom_scale(x))

This should work, however without an example data I can't test it.

CodePudding user response:

Here is how to do it using a loop and assign. I removed some redundant code/variables as this can always be a source of error. However, I agree with RobertoT that storing such data in a list and using lapply is a good idea.

samples <- paste0('sn', 1:7)

for (sn in samples) {
  
  sn.data <- get(sn)
  assign(sub('n', 'nN', sn),
         ScaleData(sn.data, features=rownames(sn.data)))
}
  • Related