Reputation: 33
x<- split(mtcars,mtcars$cyl)
sapply(x,'[',"mpg")
"From the above code, could someone explain to me why I can get the following outcome and why putting '['
in the sapply
can get the following outcome?"
$`4.mpg`
[1] 22.8 24.4 22.8 32.4 30.4 33.9 21.5 27.3 26.0 30.4 21.4
$`6.mpg`
[1] 21.0 21.0 21.4 18.1 19.2 17.8 19.7
$`8.mpg`
[1] 18.7 14.3 16.4 17.3 15.2 10.4 10.4 14.7 15.5 15.2 13.3 19.2 15.8 15.0
Upvotes: 0
Views: 56
Reputation: 4960
If you look at the args for sapply()
, you will see that the first three unnamed arguments given to it will be treated the input data (X
), the function to be applied (FUN
) and additional arguments to pass to that function (...
)
> args('sapply')
function (X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)
> help('sapply')
...
X: a vector (atomic or list) or an ‘expression’ object. Other
objects (including classed objects) will be coerced by
‘base::as.list’.
FUN: the function to be applied to each element of ‘X’: see
‘Details’. In the case of functions like ‘+’, ‘%*%’, the
function name must be backquoted or quoted.
...: optional arguments to ‘FUN’.
So when you call sapply(x,'[',"mpg")
on the list resulting from the split on mpg
, you are effectively calling the indexing operator [
on each element in the list, and passing the string mpg
to it, e.g.:
x$`4`['mpg']
mpg
Datsun 710 22.8
Merc 240D 24.4
Merc 230 22.8
Fiat 128 32.4
Honda Civic 30.4
Toyota Corolla 33.9
Toyota Corona 21.5
Fiat X1-9 27.3
Porsche 914-2 26.0
Lotus Europa 30.4
Volvo 142E 21.4
Finally, in the process of assembling the results back into a list, the names are lost, so you end up with:
$`4.mpg`
[1] 22.8 24.4 22.8 32.4 30.4 33.9 21.5 27.3 26.0 30.4 21.4
Upvotes: 4