> is.numeric_data.frame=function(x)all(sapply(x,is.numeric))
> is.numeric_data.frame(d1[[5]])
[1] TRUE
Why
d1
is a list, hence d1[5]
is a list of length 1, and in this case contains a data.frame
. to get the data frame, use d1[[5]]
.
Even if a data frame contains numeric data, it isn't numeric itself:
> x = data.frame(1:5,6:10)
> is.numeric(x)
[1] FALSE
Individual columns in a data frame are either numeric or not numeric. For instance:
> z <- data.frame(1:5,letters[1:5])
> is.numeric(z[[1]])
[1] TRUE
> is.numeric(z[[2]])
[1] FALSE
If you want to know if ALL columns in a data frame are numeric, you can use all
and sapply
:
> sapply(z,is.numeric)
X1.5 letters.1.5.
TRUE FALSE
> all(sapply(z,is.numeric))
[1] FALSE
> all(sapply(x,is.numeric))
[1] TRUE
You can wrap this all up in a convenient function:
> is.numeric_data.frame=function(x)all(sapply(x,is.numeric))
> is.numeric_data.frame(d1[[5]])
[1] TRUE