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

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.


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

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

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