i’m trying to figure out the right argument for my lapply statement

We can use colMeans directly without any loop i.e. just remove the first column which is not numeric and apply the colMeans

colMeans(life_exp[-1], na.rm = TRUE)

Or using dplyr

library(dplyr)
life_exp %>%
     summarise(across(where(is.numeric), mean, na.rm = TRUE))

Based on the OP’s code, we need the column names as argument to be looped instead of life_exp$col_name (which is not clear whether the OP created a column of column names or not). If we don’t use anonymous/lambda call, then specify the argument of the function to make sure that ‘life_exp’ is the dataset

lapply(names(life_exp)[-1], get_col_mean, data_frame = life_exp)

Or using lambda function

lapply(names(life_exp)[-1], function(nm) get_col_mean(nm, life_exp))

NOTE: The output of lapply is always a list. The function returns a numeric mean value for each column. So, if we need a vector of mean, then either unlist the lapply output or directly use sapply

sapply(names(life_exp)[-1], get_col_mean, data_frame = life_exp)

Also, this can be automated i.e. if we don’t know which are the numeric columns, then create an index first

i1 <- sapply(life_exp, is.numeric)
sapply(names(life_exp)[i1], get_col_mean, data_frame = life_exp)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top