Home > Software engineering >  How to loop through all rows and specific columns to compute a function?
How to loop through all rows and specific columns to compute a function?

Time:12-15

I am not sure why I am getting the error " longer object length is not a multiple of shorter object length"? I have run a similar code with different dataframes and it seemed to work.

 Data: 
   feature   mean   sample1  sample2 sample3
     a          3        4       8       7  
     b          4        3.4     3       6
     c          5        2       5       7

Here is the code I am working with:

      datalist6 = matrix( nrow = length(unique(Data$feature)), ncol = length(sub), 
                dimnames = list(unique(Data$feature), unique(sub)))

                sub <- colnames(Data[,c(3:46)])
                for(i in Data$feature) {
                        for (j in sub){
                        subset <- filter(Data, feature==i & sub==j)
                         func <- j/subset$mean
              datalist6[i,j] <- func
                     }
               }

This code will then throw that error. Essentially I am trying to compute coefficient of variance with mean in column subset$mean and variance of samples in columns 3:46 (sub). I am trying to run through nested for loops to determine the coefficient of variance for each row (features) for each column.

TIA!

CodePudding user response:

No for loop required.

With the assumption that you are using dplyr,

library(dplyr)
summarize(Data, feature, across(sample1:sample3, ~ . / mean))
#   feature  sample1  sample2  sample3
# 1       a 1.333333 2.666667 2.333333
# 2       b 0.850000 0.750000 1.500000
# 3       c 0.400000 1.000000 1.400000

If you instead need a base R solution,

cbind(Data["feature"], subset(Data, select = sample1:sample3) / Data$mean)
#   feature  sample1  sample2  sample3
# 1       a 1.333333 2.666667 2.333333
# 2       b 0.850000 0.750000 1.500000
# 3       c 0.400000 1.000000 1.400000
  • Related