Extract data frame from list and perform findpeaks function on each data frame

Viewed 30

I uploaded onto R multiple csv files with 16 columns that have 100 data values, some NA which I learned need to be made into 0 in order for findpeaks to work. For each data frame within the list I want to find the peaks of each individual column. I am able to run the code on an individual data frame so I know there are no issues with the data entered (code below for single data frame extracted from list).

peak_info <- lapply(myfiles$dt1, findpeaks, npeaks=6, threshold=4, nups = 2, sortstr=FALSE) 

But when I try to find peaks of all data frames saved in the list using the following code:

peaks <- lapply(myfiles, function(x)  findpeaks(x, npeaks=6, threshold=4, nups = 2, sortstr=FALSE) )

I get the following error.

Error in findpeaks(y, npeaks = 6, threshold = 4, nups = 2, sortstr = FALSE) : is.vector(x, mode = "numeric") || length(is.na(x)) == 0 is not TRUE

There are a few more functions I want to use so that is why I opted for function(x). Basically once extracted I don't need to placed the data back in the list. I find the average of the peak and saved them in a new data frame.

Thanks again for your help.

1 Answers

The error message informs that one of two conditions must be met to run the function:

  1. The x must be a vector of numeric type, or
  2. There is no NA within x.

Becuase myfiles is a list, it certainly does not meet the first condition. The error shows that myfiles doesn't meet the second condition either.

Maybe this can work as you expected:

lapply(myfiles, 
       function(x) lapply(x, 
                   findpeaks, npeaks=6, threshold=4, nups = 2,sortstr=FALSE))
Related