0

I have a list of 18 data frames that I read in using read.xlsx. Each data frame has the same number of columns but some columns contain NA for some rows. Also, in the Abundance column there are rows that contain non-numeric data and I suspect that I may need to remove these rows from each data frame but I have not been able to find a way to remove those rows.

My data frame structure is like this:

$ :'data.frame':    118 obs. of  10 variables:
  ..$ Locus                   : Factor w/ 24 levels "A","CS",..: 14 14 14 14 22 22 NA 22 10 10 ...
  ..$ Target                  : Factor w/ 96 levels "[AAAGA]14","[AAAGA]15",..: 88 91 90 87 11 12 NA 9 65 67 ...
  ..$ Length                  : num [1:118] 60 76 72 56 24 39 NA 20 139 141 ...
  ..$ Abundance               : num [1:118] 1479 1108 180 144 1786 ...
  ..$ Size             : num [1:118] 15 19 18 14 6 9.3 NA 5 32 32.2 ...
  ..$ Call                    : Factor w/ 4 levels "Al","HAs",..: 1 1 3 3 1 1 NA 3 1 1 ...
  ..$ RAR                     : num [1:118] NA 74.92 12.17 9.74 NA ...
  ..$ Position        : num [1:118] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Al.1.s.percent: num [1:118] NA NA 12.17 9.74 NA ...
  ..$ Al.2.s.percent: num [1:118] NA NA 16.2 13 NA ...

I want to apply this function to each data frame in my list of data frames.

add.sum = function(df){
    transform(df, Tot.count = ave(df[[Abundunce]], df[[Locus]], FUN = sum))
}

I tried using this line with lapply

transformed.data = lapply(mydata, add.sum)

I also tried it this way

transformed.data = lapply(mydata, function (x) add.sum(x))

But these give me the following error

Error in .subset2(x, i, exact = exact) : no such index at level 1

Any suggestions on how to get this working correctly?

aminards
  • 309
  • 2
  • 11
  • try `df[["Abundunce"]]`, `df$Abundunce` or `df[, "Abundunce"]` instead of `df[[Abundunce]]` – Bulat Sep 21 '16 at 21:05
  • Welcome to Stack Overflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – zx8754 Sep 21 '16 at 21:07
  • 2
    Your code is missing quotation marks around variable names, and Abundance is spelled incorrectly. – eipi10 Sep 21 '16 at 21:07
  • Other thing is before using a function, test it! – Bulat Sep 21 '16 at 21:08
  • @Bulat I tried using `df$Abundance` and df[, "Abundance]` but am getting the same error message. – aminards Sep 21 '16 at 21:12
  • @eipi10 Your comment about missing quotes around the variables was the problem. Thank you all for your suggestions. – aminards Sep 21 '16 at 21:18

0 Answers0