## apply function to every element in data.frame and return data.frame

UPDATE: before, I used the `paste`

function as an example instead of an arbitrary `myFun`

function. That problem was slightly easier, because `paste`

can actually operate on vectors, while `myFun`

can not.

I would like to apply my own function element-wise to every element in a data.frame, and get the modified data.frame as a return value.

Example:

> df <- data.frame(c(1,2,3), c(2,3,4)) > df c.1..2..3. c.2..3..4. 1 1 2 2 2 3 3 3 4 > df_x <- magical_apply_function(df, function(x) myFun > df_x c.1..2..3. c.2..3..4. 1 myFun(1) myFun(2) 2 myFun(2) myFun(3) 3 myFun(3) myFun(4)

I'm completely baffled to not be able to find the answer to this problem anywhere on the internet. Most resources talk about `apply`

, `lapply`

, and `sapply`

but those only work on vectors/lists and they only return lists.

Are for loops really the only way to go here?

df <- data.frame(c(1,2,3), c(2,3,4)) df[] <- lapply(df, function(x) paste(x,"x", sep="")) df

`df[]`

preserves the dataframe's structure.

**Apply function to every row in a Pandas DataFrame,** Apply function to every row in a Pandas DataFrame. Python is a great language for performing data analysis tasks. It provides with a huge amount of Classes and function which help in analyzing and manipulating data in an easier way. One can use apply() function in order to apply function to every row in given dataframe DataFrame.apply (func, axis=0, broadcast=None, raw=False, reduce=None, result_type=None, args= (), **kwds) Important Arguments are: func : Function to be applied to each column or row. This function accepts a series and returns a series. axis : Axis along which the function is applied in dataframe. Default value 0.

We can either use `mutate_all`

from `dplyr`

library(dplyr) df %>% mutate_all(funs(paste0(., "x")))

Or with `lapply`

from `base R`

and convert it to `data.frame`

data.frame(lapply(df, paste0, "x"))

**Apply a function to every row in a pandas dataframe,** Apply a function to every row in a pandas dataframe Get rid of $ and , in the SAL-RATE, then convert it to a float def money_to_float(money_str): return� To call a function for each row in an R data frame, we shall use R apply function. apply (data_frame, 1, function, arguments_to_function_if_any) The second argument 1 represents rows, if it is 2 then the function would apply on columns. Following is an example R Script to demonstrate how to apply a function for each row in an R Data Frame.

See also these `purrr`

functions

library(purrr) modify(df,paste0,"x") # output is of the same type input, so `data.frame` here # c.1..2..3. c.2..3..4. # 1 1x 2x # 2 2x 3x # 3 3x 4x map_df(df,paste0,"x") # output is always tibble # # A tibble: 3 x 2 # c.1..2..3. c.2..3..4. # <chr> <chr> # 1 1x 2x # 2 2x 3x # 3 3x 4x

**pandas.apply(): Apply a function to each row/column in Dataframe ,** Important Arguments are: func : Function to be applied to each column or row. This function accepts a series and returns a series. axis : Axis along which the function is applied in dataframe. Default value 0. If value is 0 then it applies function to each column. args : tuple / list of arguments to passed to function. Similar to map (), when you use the apply () method on a series or a data frame, the function takes each element in the series and apply the function onto the element, then returns the transformed

Can you not use `apply(df, c(1,2), myFun)`

? Using the `c(1,2)`

will apply the function to each item in your dataframe individually:

MARGIN a vector giving the subscripts which the function will be applied over. E.g., for a matrix 1 indicates rows, 2 indicates columns, c(1, 2) indicates rows and columns.

> temp<-data.frame(le=LETTERS[1:3], nu=20:22) > temp le nu 1 A 20 2 B 21 3 C 22 > apply(temp, c(1,2), function(x) {gsub('d',x,'d1d1')}) le nu [1,] "A1A1" "201201" [2,] "B1B1" "211211" [3,] "C1C1" "221221"

The function isn't used correctly if you apply the function by rows:

> apply(temp, 1, function(x) {gsub('d',x,'d1d1')}) [1] "A1A1" "B1B1" "C1C1" Warning messages: 1: In gsub("d", x, "d1d1") : argument 'replacement' has length > 1 and only the first element will be used 2: In gsub("d", x, "d1d1") : argument 'replacement' has length > 1 and only the first element will be used 3: In gsub("d", x, "d1d1") : argument 'replacement' has length > 1 and only the first element will be used

**How To: Apply Family of R Functions,** The apply function in R is used as a fast and simple alternative to loops. It allows users to apply a function to a vector or data frame by row, by column or to the entire data frame. Below are a few basic uses of this powerful function as well as one of it's sister functions lapply. apply () function apply () takes Data frame or matrix as an input and gives output in vector, list or array. apply () Function is primarily used to avoid explicit uses of loop constructs. It is the most basic of all collections can be used over a matrice. This function takes 3 arguments:

**pandas.DataFrame.apply — pandas 1.0.5 documentation,** Apply a function along an axis of the DataFrame. 1 or 'columns': apply function to each row. Returning a Series inside the function is similar to passing� Although the given responses are correct, they modify the initial data frame, which is not always desirable (and, given the OP asked for examples "using apply", it might be they wanted a version that returns a new data frame, as apply does).

**R tutorial on the Apply family of functions,** A note for the following: in most cases, R can return a value even if the You want to apply a given function to every element of a list and obtain a the use of the by clause, which sets the variable or dataframe field by which� The apply() function is used to a evaluate a function (often an anonymous one) over the margins of an array. It is most often used to apply a function to the rows or columns of a matrix (which is just a 2-dimensional array). However, it can be used with general arrays, for example, to take the average of an array of matrices.

**Apply function to each row in a DF and create a new DF with the ,** Then I have the following function which expects a dataframe with only 1 row, and it basically returns a new dataframe with just 1 row, similar to� I would even recommend that the OP modifies his data_for_time function to return a data.frame with appropriate names and types. So as to remove the as.data.frame call here and the risk of coercion. – flodel May 15 '12 at 0:11

##### Comments

- Just use
`lapply`

, q.v. the @akrun answer below. - Notwithstanding the solutions below I'm baffled why you are baffled. It's the raison d'être of data.frames to collect disparate data. Although paste works here if you can apply the function to the whole DF then usually it should be a matrix.
- bonus: the function to be applied can NOT handle vectors
- when I do something similar to this (same rationale, different function(x)) I get an Error stating that arguments imply differing number of rows: 3877, 3890, 3884, 3925, 4024, 3942, 2758, 4042, 4796, 7297 I'm using an unlist() function inside of myFun and I think it's screwing up the lengths, because its operating on a list of lists.
- holy moly, indeed, the unlist() function unlists more than just one level of listing.
- the solution to that second problem is to use unlist() with option recursive=F and then everything works out.