Questions tagged [apply]

A function to call another function with a list of arguments.

R

In the language this function calls another function with a list of arguments and a defined margin to act (either 1: rows, 2: columns or c(1,2): every entry of X).

foo = apply(X = bar, MARGIN = 1, FUN = function(x) dosomething(x))

Some similar specialized functions in the *apply-family include lapply, sapply, tapply and mapply:

  • lapply is a function that applies a function to each element of a list or vector. lapply returns a list.
  • sapply is a function that applies a function to each element of a vector (atomic or list). It may also accept other classes if they are coercible by the function base::as.list. The sapply function returns a vector by default, however will return a list when more suitable or an array if argument simplify = "array" is specified.
  • tapply is a function in the R programming language for apply a function to subsets of a vector. A vector is broken in to subsets, potentially of different lengths (aka a ragged array) based on the values of one or more other vector. The second vector is either already a factor or coerced to be a factor by as.factor. A function is applied to each of these subsets. tapply then returns either an array or a list, depending on the output of the function.
  • mapply is a multivariate version of sapply. mapply applies FUN argument to the first elements of each argument, the second elements, the third elements, and so on. Arguments are recycled if necessary.

Lisp

In this function calls another function with a list of arguments.

See the documentation for apply.

(apply #'- '(10 2))   ; -> 8

JavaScript

In this function calls another function with the provided context and the list of arguments. A common usage:

Array.prototype.forEach.apply(document.querySelectorAll("div"), [function(element){
    // do something with "element"
}]);

The MDN documentation for apply.


Python/Pandas

Use pandas specific

5923 questions
1142
votes
12 answers

Grouping functions (tapply, by, aggregate) and the *apply family

Whenever I want to do something "map"py in R, I usually try to use a function in the apply family. However, I've never quite understood the differences between them -- how {sapply, lapply, etc.} apply the function to the input/grouped input, what…
grautur
  • 29,955
  • 34
  • 93
  • 128
606
votes
8 answers

Create new column based on values from other columns / apply a function of multiple columns, row-wise in Pandas

I want to apply my custom function (it uses an if-else ladder) to these six columns (ERI_Hispanic, ERI_AmerInd_AKNatv, ERI_Asian, ERI_Black_Afr.Amer, ERI_HI_PacIsl, ERI_White) in each row of my dataframe. I've tried different methods from other…
Dave
  • 6,968
  • 7
  • 26
  • 32
548
votes
8 answers

How can I use the apply() function for a single column?

I have a pandas dataframe with multiple columns. I want to change the values of the only the first column without affecting the other columns. How can I do that using apply() in pandas?
Amani
  • 16,245
  • 29
  • 103
  • 153
250
votes
6 answers

Why isn't my Pandas 'apply' function referencing multiple columns working?

I have some problems with the Pandas apply function, when using multiple columns with the following dataframe df = DataFrame ({'a' : np.random.randn(6), 'b' : ['foo', 'bar'] * 3, 'c' : np.random.randn(6)}) and the…
Andy
  • 9,483
  • 12
  • 38
  • 39
239
votes
7 answers

python pandas: apply a function with arguments to a series

I want to apply a function with arguments to a series in python pandas: x = my_series.apply(my_function, more_arguments_1) y = my_series.apply(my_function, more_arguments_2) ... The documentation describes support for an apply method, but it…
Abe
  • 22,738
  • 26
  • 82
  • 111
223
votes
13 answers

Return multiple columns from pandas apply()

I have a pandas DataFrame, df_test. It contains a column 'size' which represents size in bytes. I've calculated KB, MB, and GB using the following code: df_test = pd.DataFrame([ {'dir': '/Users/uname1', 'size': 994933}, {'dir':…
PaulMest
  • 12,925
  • 7
  • 53
  • 50
220
votes
4 answers

When should I (not) want to use pandas apply() in my code?

I have seen many answers posted to questions on Stack Overflow involving the use of the Pandas method apply. I have also seen users commenting under them saying that "apply is slow, and should be avoided". I have read many articles on the topic of…
cs95
  • 379,657
  • 97
  • 704
  • 746
200
votes
14 answers

Remove columns from dataframe where ALL values are NA

I have a data frame where some of the columns contain NA values. How can I remove columns where all rows contain NA values?
Gnark
  • 4,080
  • 7
  • 33
  • 44
166
votes
5 answers

Is R's apply family more than syntactic sugar?

...regarding execution time and / or memory. If this is not true, prove it with a code snippet. Note that speedup by vectorization does not count. The speedup must come from apply (tapply, sapply, ...) itself.
steffen
  • 2,152
  • 4
  • 19
  • 30
159
votes
7 answers

Apply a function to every row of a matrix or a data frame

Suppose I have a n by 2 matrix and a function that takes a 2-vector as one of its arguments. I would like to apply the function to each row of the matrix and get a n-vector. How to do this in R? For example, I would like to compute the density of a…
Tim
  • 1
  • 141
  • 372
  • 590
150
votes
3 answers

Apply function to each cell in DataFrame

I have a dataframe that may look like this: A B C foo bar foo bar bar foo foo bar I want to look through every element of each row (or every element of each column) and apply the following function to get the…
eljusticiero67
  • 2,257
  • 4
  • 15
  • 18
147
votes
4 answers

Is the "*apply" family really not vectorized?

So we are used to say to every R new user that "apply isn't vectorized, check out the Patrick Burns R Inferno Circle 4" which says (I quote): A common reflex is to use a function in the apply family. This is not vectorization, it is loop-hiding.…
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
100
votes
6 answers

R Apply() function on specific dataframe columns

I want to use the apply function on a dataframe, but only apply the function to the last 5 columns. B<- by(wifi,(wifi$Room),FUN=function(y){apply(y, 2, A)}) This applies A to all the columns of y B<-…
skmathur
  • 1,587
  • 5
  • 14
  • 21
98
votes
5 answers

How does the Math.max.apply() work?

How does the Math.max.apply() work?. JS Bin