How to apply a mathematical function to a list of objects in R?

r apply function with multiple arguments
apply() function to list r
r lapply custom function
apply function in r
r apply dataframe
r function
r apply function to each row
mapply function in r

So I have a large list of objects (200+) which only contain a numeric value. I want to apply the same mathematical (get the 80% of the value) function for all of them, but it's not working out for me.

Here's a small example.

a = 680

b = 820

c = 1040

list = as.list(ls())

fun = function(x){x*.8}

for (i in list){
  fun(i)
}

And I get an error saying non-numeric argument to binary operator. I get that this might be happening because the software is applying the function to the names in the list and no the numerical values, but for the life of me I can't make it work and haven't been able to find this exact issue online. I know the answer must be so simple but I'm a newbie, really. Any help is appreciated.

Thank you!!

You are looking for the mget function: This is a multivariate format of get

thus:

a = 680

b = 820

c = 1040

list = mget(ls())## This is the part you need to change

fun = function(x){x*.8}

for (i in list){
  fun(i)
}

R: Apply a Function over a List or Vector, Other objects (including classed objects) will be coerced by as.list . FUN, the function to be applied to each element of X : see Details. In the case of functions  The object on which the function has to be applied: In this case, it’s the matrix counts. The dimension or index over which the function has to be applied: The number 1 means row-wise, and the number 2 means column-wise. Here, we apply the function over the columns.

You get an error because ls() only returns the names of the variables you've defined. ("a", "b" and "c")

Having said that, this example could be more easily be done using regular vector and scalar multiplication.

x = c(680, 820, 1040)
x * .8

If for some reason you want you use a list and function anyway I would recommend using lapply, like so:

a = 680
b = 820
c = 1040

list = list(a, b, c)

fun = function(x){ x*.8 }

lapply(list, fun)

lapply , sapply, a vector (atomic or list) or an expression object. Other objects (including classed objects) will be coerced by base::as.list . FUN. the function to  Apply a Function over a List or Vector Description. lapply returns a list of the same length as X. Each element of which is the result of applying FUN to the corresponding element of X. sapply is a ``user-friendly'' version of lapply also accepting vectors as X, and returning a vector or array with dimnames if appropriate. Usage

Use eapply to apply a function to all variables in an environment. However, I recommend you to first build a list of numeric variables and use lapply(myList, fun) as variables in an environment can easily be manipulated unintentionally. Refer to the code below:

##### Solution 1 using eapply() #####
# Build a new temporary environment to save all numeric variables
tempEnv <- new.env()
tempEnv$a = 680
tempEnv$b = 820
tempEnv$c = 1040

fun = function(x){x*.8}

# apply fun() to all variables in "tempEnv"
eapply(tempEnv, fun)


##### Solution 2 using lapply() (Recommended) #####
# Define all variables in a list
myList <- list(a = 680, 
               b = 820, 
               c = 1040)

# apply fun() to all values in myList
lapply(myList, fun)

lapply: Apply a Function over a List or Vector, a vector (atomic or list) or an expression object. Other objects (including classed objects) will be coerced by base::as.list . FUN. the function to  Here’s a selection of statistical functions that come with the standard R installation. You’ll find many others in R packages. Central Tendency and Variability Function What it Calculates mean(x) Mean of the numbers in vector x. median(x) Median of the numbers in vector x var(x) Estimated variance of the population from which the numbers in […]

rapply: Recursively Apply a Function to a List, In R 3.5.x and earlier, object was required to be a list, which was not the case for its list-like components. Value. apropos – Return character vector with names of objects that contain the input. arrange [dplyr] – Order data frames and tibbles. attach – Give access to variables of a data.frame. attr – Return or set a specific attribute of a data object. attributes – Return or set all attributes of a data object.

R tutorial on the Apply family of functions, Lists. A list is an R structure that may contain object of any other types, including other lists. Lots of the modeling functions (like t.test() for the t test or lm() for  help(package=graphics) # List all graphics functions plot() # Generic function for plotting of R objects par() # Set or query graphical parameters curve(5*x^3,add=T) # Plot an equation as a curve points(x,y) # Add another set of points to an existing graph arrows() # Draw arrows [see errorbar script] abline() # Adds a straight line to an existing graph lines() # Join specified points with line

Lists in R, In this post, I am going to discuss the efficiency of apply functions over loops from the input object and the function specified. apply() can return a vector, list, matrix or list1<-list(maths=c(64,45,89,67),english=c(79,84,62,80)  A list can be converted to a vector so that the elements of the vector can be used for further manipulation. All the arithmetic operations on vectors can be applied after the list is converted into vector. To do this conversion, we can use the unlist () function. It takes the list as input and produces a vector.

Comments
  • change your list to list = mget(ls()) instead of as.list use mget then you can be able to do all the other operations
  • This is it. I appreciate Jason's and vectorson's answers, but having 200+ elements in my environment I don't want the create the vector by hand. I didn't know the "mget" function. Thank you so much!
  • This is it! Thank you Onyambu. Can't upvote yet (too new), but this was the solution for me.