Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is.numeric within apply vs sapply

Tags:

r

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?

like image 478
KirkD-CO Avatar asked May 04 '16 20:05

KirkD-CO


People also ask

What is the difference between apply and Sapply in R?

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.

What does Sapply do in R?

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.

What is the use of apply family functions in R explain in detail?

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.


2 Answers

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 
like image 70
Señor O Avatar answered Sep 29 '22 11:09

Señor O


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.

like image 40
user5249203 Avatar answered Sep 29 '22 10:09

user5249203