Debugging lapply/sapply calls
Use the standard R debugging techniques to stop exactly when the error occurs:
options(error = browser)
or
options(error = recover)
When done, revert to standard behaviour:
options(error = NULL)
Unexpected behaviour of function used in conjunction with lapply/sapply?
As we know functions by default return the last line in the function, however, since a value is assigned in this function it doesn't explicitly display the result but if you use print
you can see it.
print(mult_six(7))
#[1] 42
Need help converting a for loop to lapply or sapply
You can rbind
all elements in a list using do.call
, and you can read in all the files into that list using lapply
:
mean(
filter( # here's the filter that will be applied to the rbind-ed data
do.call("rbind", # call "rbind" on all elements of a list
lapply( # create a list by reading in the files from list.files()
# add any necessary args to read.csv:
list.files("[::DIR_PATH::]"), function(x) read.csv(file=x, ...)
)
)
), ID %in% id)$pollutant, # make sure id is replaced with what you want
na.rm = TRUE
)
R: sapply / lapply Different Behaviour with Names
Look carefully at this:
lapply(cc, ff)
#> [[1]]
#> [[1]]$myname
#> [1] "1"
#>
#>
#> [[2]]
#> [[2]]$myname
#> [1] "2"
The output of lapply
itself doesn't have names. Look:
a <- lapply(cc, ff)
names(a)
#> NULL
The output of the lapply
is actually an unnamed list. Each element of a
is a named list.
names(a[[1]])
#> [1] "myname"
names(a[[2]])
#> [1] "myname"
So in fact, USE.NAMES
will apply, and sapply
will assign the contents of cc
as names for the output of the lapply
for which sapply
is a thin wrapper as stated in the documentation. It's quite straightforward to follow the code through:
sapply
#> function (X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)
#> {
#> FUN <- match.fun(FUN)
#> answer <- lapply(X = X, FUN = FUN, ...)
#> if (USE.NAMES && is.character(X) && is.null(names(answer)))
#> names(answer) <- X
#> if (!isFALSE(simplify) && length(answer))
#> simplify2array(answer, higher = (simplify == "array"))
#> else answer
#> }
#> <bytecode: 0x036ae7a8>
#> <environment: namespace:base>
Using lapply to nested list
It seems like a nested list
of vector
s. We may use recursive apply (rapply
)
rapply(qw, f = \(x) norm(matrix(x)))
Or use a nested sapply/lapply
as.vector(sapply(qw, \(x) sapply(x, \(y) norm(matrix(y)))))
How to tell lapply to ignore an error and process the next thing in the list?
Use a tryCatch
expression around the function that can throw the error message:
testFunction <- function (date_in) {
return(tryCatch(as.Date(date_in), error=function(e) NULL))
}
The nice thing about the tryCatch
function is that you can decide what to do in the case of an error (in this case, return NULL
).
> lapply(dates2, testFunction)
[[1]]
[1] "2010-04-06"
[[2]]
NULL
[[3]]
[1] "2010-04-08"
Related Topics
Select Along One of N Dimensions in Array
Identify Records in Data Frame a Not Contained in Data Frame B
Change Path.Expand Location (Win 7)
Get Decision Tree Rule/Path Pattern for Every Row of Predicted Dataset for Rpart/Ctree Package in R
Why I Get This Error Writing Data to a File
Image Not Showing in Shiny App R
How to Combine Scales for Colour and Size into One Legend
Error Creating R Data.Table with Date-Time Posixlt
Calculating Time Difference Between Two Columns
Adding Empty Graphs to Facet_Wrap in Ggplot2
Why the Built-In Lm Function Is So Slow in R
Interactive Directory Input in Shiny App (R)
Show Correlations as an Ordered List, Not as a Large Matrix