3 ms·
> After reading your explanation I still, as has been the case for years, don't understand what sapply or rapply does, There is nothing complicated about what
by nonbel 7y ago
> After reading your explanation I still, as has been the case for years, don't understand what sapply or rapply does,
There is nothing complicated about what sapply does... It simply means loop over the elements of the first argument (which must be a list; btw a dataframe, df, is internally the same as a list) and apply some function. lapply does this and returns a list, sapply does this and can optionally "simplify" the results into a vector, etc.
So:
lapply(df, class) = loop over the elements of df and tell me the class, return this in the form of a list
sapply(df, class) = loop over the elements of df and tell me the class, return this as a character vector
This is basically lapply:
res = NULL
for(i in 1:length(df)){
res = append(res, class(df[i]))
}
return(res)