简体   繁体   English

将 function 应用于嵌套列表的每个元素 r

[英]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我们可以使用来自mappurrr

library(purrr)
map(a, exp)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM