1

I have a function like this:

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)
2
  • 1
    What is your expected output? lapply(a, exp) will return a list of exponentiated vectors. Commented Apr 23, 2020 at 15:19
  • If I want to apply a function like 10^x? Commented Apr 23, 2020 at 16:50

3 Answers 3

1

e doesn't exist as a constant in R, but the exponential function does.
Just use exp_a <- lapply(a, exp)

Sign up to request clarification or add additional context in comments.

2 Comments

I would like to use a customized function like 10 ^x
What is the problem then? lapply(a, function(x)10^x) will do that. Or do you need a different return format?
1

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))))

Comments

0

We can use map from purrr

library(purrr)
map(a, exp)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.