Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: sapply works but lapply doesn't

I am trying to better understand the difference between lapply and sapply. In the example below, why does the later work, but not the former?

# Data

  data <- data.frame("A" = rnorm(500, 0, 1), 
                     "B" = rnorm(500, 0, 1), 
                     "C" = sample(c("a", "b" , "c"), 500, replace = T))

# Give the mean of all numeric elements in the dataframe. 

  lapply(data[lapply(data, is.numeric)], mean) # Doesn't work

  Error in `[.default`(data, lapply(data, is.numeric)) : 
  invalid subscript type 'list'

  lapply(data[sapply(data, is.numeric)], mean) # Works
like image 839
D L Dahly Avatar asked Dec 05 '25 10:12

D L Dahly


2 Answers

lapply returns a list by default:

From documentation:

lapply returns a list of the same length as X, each element of which is the result of applying FUN to the corresponding element of X.

sapply returns a vector by default:

From documentation:

sapply is a user-friendly version and wrapper of lapply by default returning a vector.

So, when you slice/subset a data.frame like this data[sapply(data, is.numeric)] you need to pass a vector of elements otherwise it wont work. And that's why sapply works and lapply doesn't.

like image 115
LyzandeR Avatar answered Dec 07 '25 07:12

LyzandeR


It isn't working because you are trying to subset with a list, hence the error message you omitted. If you have your heart set on using the double lapply you can unlist the inner part.

lapply(data[unlist(lapply(data, is.numeric))], mean)
like image 32
cdeterman Avatar answered Dec 07 '25 06:12

cdeterman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!