Chris
Chris

Reputation: 2071

how to apply lapply() with a function with arguments

I have this df:

set.seed(20)
df <- data.frame(X1 = sample(c(1:10,NA), 10, replace=TRUE),
                X2 = sample(c(1:10,NA), 10, replace=TRUE),
                X3 = sample(c(1:10,NA), 10, replace=TRUE),
                stringsAsFactors = FALSE)

> df
   X1 X2 X3
1  10  8  6
2   9  9  1
3   4  1  5
4   6  9  1
5  NA  3  3
6  NA  5  1
7   2  4 10
8   1  2 NA
9   4  4  1
10  5 10  8

Where I can use this functions easy:

lapply(df, sum)
df %>% lapply(., sum)
df %>% lapply(., as.numeric)

However, if I want to put na.rm=TRUE argument in sum() is impossible. I have been looking for an answer and it seems the only solution is to define the function sum inside lapply(), like:

lapply(df, function() {})

Is it really no possible to put the arguments of the function FUN inside lapply? Also, the problem I'm struggling with is when I want to apply a function which needs the data (for example, sum(data, na.rm=TRUE)) using pipe operators I cant give the data to the function as:

df %>% lapply(., sum(, na.rm=TRUE)) # It needs the sum argument.
df %>% lapply(., sum(., na.rm=TRUE)) # but I'm not looking to sum the whole df

Upvotes: 0

Views: 645

Answers (1)

yarnabrina
yarnabrina

Reputation: 1666

I guess you want to sum the columns of df here. You can do it as below:

set.seed(seed = 20)

df <- data.frame(X1 = sample(c(1:10, NA), 10, replace = TRUE),
                 X2 = sample(c(1:10, NA), 10, replace = TRUE),
                 X3 = sample(c(1:10, NA), 10, replace = TRUE))

df
#>    X1 X2 X3
#> 1  10  8  6
#> 2   9  9  1
#> 3   4  1  5
#> 4   6  9  1
#> 5  NA  3  3
#> 6  NA  5  1
#> 7   2  4 10
#> 8   1  2 NA
#> 9   4  4  1
#> 10  5 10  8

lapply(df, sum, na.rm = TRUE)
#> $X1
#> [1] 41
#> 
#> $X2
#> [1] 55
#> 
#> $X3
#> [1] 36

Created on 2019-04-02 by the reprex package (v0.2.1)

An alternative will be to use colSums(df, na.rm = TRUE).

Upvotes: 1

Related Questions