apply is for arrays, e.g., if you have a matrix and want to apply a function to each row or each column. The result is another array---*if* the function you used returned the same number of elements for each row/column/whatever. Otherwise it's a list (and a mess).
Bottom line: lapply is the safest one of the bunch. List (or vector) in, list out.
sapply should always be used with care---it's basically lapply, followed by an attempt to simply the result into an array. That simplification process is what tends to go awry.
lapply is for lists---apply a function to each element of a list. The result is always a list.
tapply is basically split + lapply. You use it when you want a function to act on subsets of the input vector that are defined by a factor.
when you define function(x) as { GetValues (x) }
why not just call getValues?
i.e.
sapply(yearList, getValues)
contingocoins 1 year ago
apply is for arrays, e.g., if you have a matrix and want to apply a function to each row or each column. The result is another array---*if* the function you used returned the same number of elements for each row/column/whatever. Otherwise it's a list (and a mess).
Bottom line: lapply is the safest one of the bunch. List (or vector) in, list out.
joeionnojitsu 2 years ago
sapply should always be used with care---it's basically lapply, followed by an attempt to simply the result into an array. That simplification process is what tends to go awry.
lapply is for lists---apply a function to each element of a list. The result is always a list.
tapply is basically split + lapply. You use it when you want a function to act on subsets of the input vector that are defined by a factor.
joeionnojitsu 2 years ago