Reputation: 11128
I have a dataset which contain some 200 fields and 1000000 of records as per below format:
Acc Field1 Field2 Field3 .....
101 23 34 78
102 6 1.2 89
.
.
.
When I enter command
apply(dat3[varlist[9]],2,is.numeric)
I get confirmation of field name with TRUE as a result, but when I try to test as is.numeric(dat3[varlist[9]])
, I receive a FALSE as a result.
where dat3 is the dataframe on which I am working on and varlist is created using command:
varlist <- names(dat3)
varlist contains all the list of variables in the data frame.
Attached is the real time screenshot.
I don't understand what I am doing wrong over here.
Any help from anybody is appreciated.
Upvotes: 0
Views: 153
Reputation: 6207
If you do
varlist <- names(iris)
iris[varlist[1]]
you are defining a new dataframe with the selected column and not the variable itself.
do
iris[,colnames(iris) %in% varlist[1]]
is.numeric( iris[,colnames(iris) %in% varlist[1]])
#[1] TRUE
And try to make a reproducible example next time please
Upvotes: 1
Reputation: 121187
The standard way to perform tests column by column on a data frame is to use sapply
.
sapply(mtcars, is.numeric)
sapply(CO2, is.numeric)
(Try swapping is.numeric
for class
as well.)
Upvotes: 4
Reputation: 66874
Using [
on a data.frame in this way will return another (smaller data frame). You need to use [[
, or use the multi-index version of [
so that dropping is applied. For example:
class(mtcars[3])
[1] "data.frame"
is.numeric(mtcars[3])
[1] FALSE
is.numeric(mtcars[[3]])
[1] TRUE
is.numeric(mtcars[,3])
[1] TRUE
Upvotes: 2