[英]apply function to each element of a list nested r
I have a function like this:我有一个这样的 function:
a <- list()
a$prod_1 <- c(1,2,3)
a$prod_2 <- c(4,5,6)
and I want to exponentiante each element of the list I tried something like this:我想对列表中的每个元素取幂 我试过这样的事情:
exp_a <- lapply(a, funtion(x) e^x)
e
doesn't exist as a constant in R, but the exp
onential function does. e
在 R 中不作为常数存在,但exp
是。
Just use exp_a <- lapply(a, exp)
只需使用
exp_a <- lapply(a, exp)
You have a couple of ways to make it, while below are just two examples:你有几种方法可以做到,而下面只是两个例子:
exp_a <- Map(exp,a)
or要么
exp_a <- as.list(as.data.frame(exp(do.call(cbind,a))))
We can use map
from purrr
我们可以使用来自
map
的purrr
library(purrr)
map(a, exp)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.