In writing R code to identify columns with numeric data, I had some issues and found this question on SO: Is there a vectorized way to use is.numeric?
Within that page I found my answer, but I'm puzzled by the nature of the problem itself. On that page, the following code snip does what I expect:
> dat <- data.frame(v1=1:5,v2=letters[1:5],v3=rnorm(5),v4=c(1,2,'c','d',5))
> sapply(dat,is.numeric)
v1 v2 v3 v4
TRUE FALSE TRUE FALSE
But if I change sapply to apply, I get to what caused my problem.
> dat <- data.frame(v1=1:5,v2=letters[1:5],v3=rnorm(5),v4=c(1,2,'c','d',5))
> apply(dat,2,is.numeric)
v1 v2 v3 v4
FALSE FALSE FALSE FALSE
Why do apply and sapply result in different evaluations?
Difference between lapply() and sapply() functions:lapply() function displays the output as a list whereas sapply() function displays the output as a vector. lapply() and sapply() functions are used to perform some operations in a list of objects.
sapply() function in R Language takes list, vector or data frame as input and gives output in vector or matrix. It is useful for operations on list objects and returns a list object of same length of original set.
The apply() family pertains to the R base package and is populated with functions to manipulate slices of data from matrices, arrays, lists and dataframes in a repetitive way. These functions allow crossing the data in a number of ways and avoid explicit use of loop constructs.
apply
coerces everything into the same type. Numbers can become characters but not vice versa. So everything is coerced to character
for apply to work.
> apply(dat[,1, drop = F], 2, is.numeric)
v1
TRUE
apply
is not advisable for data frames as it will coerce to a matrix. Matrix,as you probably are aware all columns in a matrix must have the same mode(numeric, character, etc.)... by default apply
converts the columns into character.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With