Skip to content
Prev 360680 / 398503 Next

Antwort: Re: selecting columns from a data frame or data table by type, ie, numeric, integer

Hi Martin and list:
First let me thank you for thinking of me.?? It is probably apparent that my programming experience is limited, and the vector aspect of R has taken some getting used to.?? A very very long time ago I did some programming in Fortran and for loops and if statements were ordinary, useful, and used frequently.?? Now if I see a for loop and if statement together then it is flatly apparent that I need to rework that code to take advantage of R's strengths using vectoized functions on whatever object I am working on.??
It has also become apparent that one should read manuals, experiment with some code to firmly cement the knowledge, and build a solid foundation.?? My natural inclination is the opposite.? I am anxious to produce some code to solve a problem or satisfy curiosity or ....? R is not something one can learn and use productively in a few weeks or months.?? It is powerful, subtle, and takes some thinking.?? I started this trek into R with Jared Lander's book "R for Everyone",? progressed to Prof Norman Matloff's "The Art of R Programming" (which was way beyond my comprehension at the beginning) and Hadley Wickham's "ggplot2" book, and several courses with Data Camp and Couresra.? One day I will be at the point where I do know what I don't know about R and at that time I will almost be competent with the language.

I have taken the work from Bill Dunlap and Giorgio Garziano and have applied it to my little project and am just amazed that so little code can do so much.? I have also followed Bert Gunter's advice and taken that code and dissected it item by item to comprehend what each element is doing.?? 

The knowledge and help on this list is just amazing and I do appreciate the efforts of all involved.? I read the digest daily .
Carl Sutton CPA
On Wednesday, May 4, 2016 12:06 AM, Martin Maechler <maechler at stat.math.ethz.ch> wrote:
>>>>>? <G.Maubach at weinwolf.de>
Ouch -- so many problems in such a short piece of R code !!!
Sorry, but after reading the above, I'd strongly recommend getting
better books about R...
? ? ? {{maybe do not take those containing "data science" ;-)}}

Compared to the nice and efficient solution of Bill Dunlap,
the above is really bad-bad-bad? in at least four ways :

0) They way you write it above, you cannot use it,
? ? <string> == "variant1|variant2|..."
? is pseudocode and does not really work

1) Note the missing "[, i]"? in the 2nd line: It should be
? ? if(class(dataset[, i]) ...

2) A for loop changing each column at a time is really slow for
? largish data sets

3) [last but not at all least!]
? Please ... many of you readers, do learn:
? 
 Using checks such as
? ? ? if ( class(x) == "numeric" )
 are (almost) always wrong by design !!!

 Instead you really should (almost) always use

 ??? if(inherits(x, "numeric"))

Why?? Because classes in R (S3 or S4) can *extend* other classes.
Example: Many of you know that after? fm <- glm(...)
class(fm) is? c("glm", "lm")? and so

? ? > if(class(fm) == "lm")
? ? + "yes"
? ? Warning message:
? ? In if (class(fm) == "lm") "yes" :
? ? ? the condition has length > 1 and only the first element will be used

Similarly, in your case

y <- 1:10
class(y) <- c("myNumber", "numeric")

when that 'y' is a column in your data frame,
the test for? if(class(dataset[,i]) == "numeric")? will *not*
work but actually produce the above warning.

However, one? could als have had

Num <- setClass("Num", contains="numeric")
N <- Num(1:10)

? ? > Num <- setClass("Num", contains="numeric")
? ? > N <- Num(1:10)
? ? > N
? ? An object of class "Num"
? ? ? [1]? 1? 2? 3? 4? 5? 6? 7? 8? 9 10
? ? > if(class(N) == "numeric") "yes" else "no"
? ? [1] "no"
? ? > 

I hope that many of the readers --- including *MANY* authors of
R packages !! --- have understood the above and will fix their R
code -- and even more their books where applicable !!

Martin Maechler,
ETH Zurich & R Core Team