Reputation: 1572
I need to apply a function to a nested list. The structure of the first list item is :
str(ldf[[1]])
List of 3
$ Header :List of 10
..$ abif : chr "ABIF"
..$ version : int 101
..$ DirEntry.name : raw [1:4] 74 64 69 72
..$ DirEntry.number : int 1
..$ DirEntry.elementtype: int 1023
..$ DirEntry.elementsize: int 28
..$ numelements : int 99
..$ dataoffset : int 97865
..$ datahandle : int 0
..$ unused : num [1:47] 0 0 0 0 0 0 0 0 0 0 ...
$ Directory:'data.frame': 99 obs. of 7 variables:
..$ name : chr [1:99] "ANME" "CMNT" "CMNT" "CMNT" ...
..$ tagnumber : int [1:99] 1 1 2 3 4 5 1 1 1 1 ...
..$ elementtype: int [1:99] 19 18 18 18 18 18 19 19 19 18 ...
..$ elementsize: int [1:99] 1 1 1 1 1 1 1 1 1 1 ...
..$ numelements: int [1:99] 13 29 29 29 29 29 10 10 4 9 ...
..$ datasize : int [1:99] 13 29 29 29 29 29 10 10 4 9 ...
..$ dataoffset : int [1:99] 140 97720 97749 97778 97807 97836 97686 97696 1346454016 95088 ...
$ Data :List of 99
..$ ANME.1 : chr "AFLP-Default"
..$ CTID.1 : chr "GS#12-081"
..$ CTNM.1 : chr "GS#12-081"
..$ CTOw.1 : chr "PAB"
..$ CTTL.1 : chr "Comment:"
..$ CpEP.1 : chr "\001"
..$ DATA.1 : int [1:8960] 1 -2 -3 -3 -2 2 0 0 1 2 ...
..$ DATA.2 : int [1:8960] -2 3 4 5 2 -3 0 0 -2 -1 ...
..$ DATA.3 : int [1:8960] 0 0 -6 -6 -2 3 -5 1 3 1 ...
..$ DATA.4 : int [1:8960] -1 -1 3 2 1 -5 3 -4 -4 -6 ...
..$ DATA.5 : int [1:544] 0 0 0 0 0 0 0 0 0 0 ...
..$ DATA.6 : int [1:544] 0 0 0 0 0 0 0 0 0 0 ...
..$ DATA.7 : int [1:544] 6 6 6 6 6 6 15 15 15 15 ...
..$ DATA.8 : int [1:544] 60 60 60 59 59 59 59 59 59 59 ...
Let's say I'm interested in the values in $Data$DATA.1. How to apply a function to every $Data$DATA.1 ? Like :
findpeaks(ldf[[1]]$Data$DATA.1,threshold=500)
findpeaks(ldf[[2]]$Data$DATA.1,threshold=500)
findpeaks(ldf[[x]]$Data$DATA.1,threshold=500)
...
From : How do you apply a function to a nested list? I get :
lapply(ldf, function(x) lapply(lapply(x, '[[', 'Data'), function(x) findpeaks(ldf[[x]]$Data$DATA.1,threshold=500)))
Error in ldf[[x]] : attempt to select less than one element
Is this list to deeply nested to use lapply ?
Upvotes: 1
Views: 2200
Reputation: 1572
From joran's comment, here's the working code :
lapply(ldf,function(x) findpeaks(x$Data$DATA.1,threshold=500))
Upvotes: 2