Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- A <- as.list(0:9)
- B <- as.list(0:9)
- f <- function(x, y) x^2+y
- OUT <- list()
- for (n in 1:10) OUT[[n]] <- f(A[[n]], B[[n]])
- OUT
- [[1]]
- [1] 0
- [[2]]
- [1] 2
- ...
- zip <- function(x, y){
- stopifnot(length(x)==length(y))
- z <- list()
- for (i in seq_along(x)){
- z[[i]] <- list(x[[i]], y[[i]])
- }
- z
- }
- E <- zip(A, B)
- lapply(E, function(x) f(x[[1]], x[[2]]))
- [[1]]
- [1] 0
- [[2]]
- [1] 2
- ...
- ‘mapply’ is a multivariate version of ‘sapply’. ‘mapply’ applies
- ‘FUN’ to the first elements of each ... argument, the second
- elements, the third elements, and so on. Arguments are recycled
- if necessary.
- OUT <- lapply(1:10, function(x) (A[[x]]^2 + B[[x]]))
- OUT <- lapply(1:10, function(x) f(A[[x]], B[[x]]))
- en = c("cattle", "chicken", "pig")
- zh = c("牛", "鸡", "猪")
- dict <- new.env(hash = TRUE)
- Add <- function(key, val) dict[[key]] <- val
- mapply(Add, en, zh)
- ## cattle chicken pig
- ## "牛" "鸡" "猪"
Add Comment
Please, Sign In to add comment