0

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.

enter image description here

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
PKumar
  • 10,971
  • 6
  • 37
  • 52

3 Answers3

4

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.)

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
2

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
James
  • 65,548
  • 14
  • 155
  • 193
1

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

Jonas Tundo
  • 6,137
  • 2
  • 35
  • 45
  • `iris[varlist[1]]` and `iris[,colnames(iris) %in% varlist[1]] ` are rather perverse indexing. Just use `iris[, 1]` or `iris[[1]]`. – Richie Cotton Oct 22 '13 at 10:41
  • No problem. You can use datasets like `iris` and `mtcars` that are already available in R (or other ones, run `data()` to see a list of available datasets). Also take a look at http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Jonas Tundo Oct 22 '13 at 10:43
  • @Richie I know, but I just wanted to start from his approach to show the difference. – Jonas Tundo Oct 22 '13 at 10:44