Skip to content

Filtering a data frame using a string for colum header

5 messages · Chunhao Tu, PIKAL Petr, Brian Ripley +1 more

#
Hi all,

I was just radomly playing with R and got the following error when
trying to filter a data frame using a string:
DrHorrible
1         10
2          9
3          9
4         10
5         10
Error in -"DrHorrible" : invalid argument to unary operator
I know how to work around this problem quite easily, I'm just curious
as to why the my.df[-"DrHorrible] statement didn't work?

Cheers,
Tony

OS = win XP
R version 2.8.1 (2008-12-22)
i386-pc-mingw32

locale:
LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United Kingdom.
1252;LC_MONETARY=English_United Kingdom.
1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods
base
#
Hi

r-help-bounces at r-project.org napsal dne 23.02.2009 03:44:41:
would
must
It is a bit deeper than [] selection.
Error in -"DrHorrible" : invalid argument to unary operator, so the error 
is within selection operator [...].

If you know the name and you do not want column with this particular name 
use

my.df[,!names(my.df)==("DrHorrible")]

Regards
Petr
Kingdom.
http://www.R-project.org/posting-guide.html
http://www.R-project.org/posting-guide.html
#
I would have used

1) != rather than ! ... ==

2) !(names(my.df) %in% "DrHorrible")  since that handles NA names 
(possible, but not easy to get) better.

and note that

3) subset(my.df, select = -DrHorrible) works.
On Mon, 23 Feb 2009, Petr PIKAL wrote:

            

  
    
#
Cheers guys,

yeah, I was using this bit of code as my work around:
but you guys had better solutions  :-)

Is there an R wish list somewhere? I tried google but couldn't find a
specific location, just various threads here and there. It just seems
to me that if
works, then
should work too, in the same way that my.df2[-2] would.

Thanks again, always good to learn new and better ways of doing
things,
Tony Breyal
On 23 Feb, 08:41, Prof Brian Ripley <rip... at stats.ox.ac.uk> wrote: