Re: [R] Filtering a data frame using a string for colum header
Hi Tony, I "GUESS" my.df[-"DrHorrible"] does not tell R which column "NUMBER" would like to remove. As we know, we could use my.df[-4] and it exactly tells R which column must remvoer from your data. > my.df[-4] Angel Buffy Firefly 1 7 8 9 2 8 9 9 3 6 4 10 4 9 9 10 51010 10 I may be not right. Chunhao > Date: Sun, 22 Feb 2009 14:48:10 -0800 > From: tony.bre...@googlemail.com > To: r-help@r-project.org > Subject: [R] Filtering a data frame using a string for colum header > > Hi all, > > I was just radomly playing with R and got the following error when > trying to filter a data frame using a string: > > > Angel <- c(7,8,6,9,10) > > Buffy <- c(8,9,4,9,10) > > Firefly <- c(9,9,10,10,10) > > DrHorrible <- c(10,9,9,10,10) > > my.df <- data.frame(Angel, Buffy, Firefly, DrHorrible) > > my.df["DrHorrible"] > DrHorrible > 1 10 > 2 9 > 3 9 > 4 10 > 5 10 > > my.df[-"DrHorrible"] > 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 > > sessionInfo() > 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 > > > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. _ [[elided Hotmail spam]] [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Filtering a data frame using a string for colum header
Hi r-help-boun...@r-project.org napsal dne 23.02.2009 03:44:41: > > Hi Tony, > > I "GUESS" my.df[-"DrHorrible"] does not tell R which column "NUMBER" would > like to remove. > As we know, we could use my.df[-4] and it exactly tells R which column must > remvoer from your data. > > > my.df[-4] > Angel Buffy Firefly > 1 7 8 9 > 2 8 9 9 > 3 6 4 10 > 4 9 9 10 > 51010 10 > > > I may be not right. It is a bit deeper than [] selection. > -"DrHorrible" 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 > > > > Chunhao > > > > > > > > > > Date: Sun, 22 Feb 2009 14:48:10 -0800 > > From: tony.bre...@googlemail.com > > To: r-help@r-project.org > > Subject: [R] Filtering a data frame using a string for colum header > > > > Hi all, > > > > I was just radomly playing with R and got the following error when > > trying to filter a data frame using a string: > > > > > Angel <- c(7,8,6,9,10) > > > Buffy <- c(8,9,4,9,10) > > > Firefly <- c(9,9,10,10,10) > > > DrHorrible <- c(10,9,9,10,10) > > > my.df <- data.frame(Angel, Buffy, Firefly, DrHorrible) > > > my.df["DrHorrible"] > > DrHorrible > > 1 10 > > 2 9 > > 3 9 > > 4 10 > > 5 10 > > > my.df[-"DrHorrible"] > > 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 > > > sessionInfo() > > 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 > > > > > > > __ > > R-help@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > _ > [[elided Hotmail spam]] > >[[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Filtering a data frame using a string for colum header
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: Hi r-help-boun...@r-project.org napsal dne 23.02.2009 03:44:41: Hi Tony, I "GUESS" my.df[-"DrHorrible"] does not tell R which column "NUMBER" would like to remove. As we know, we could use my.df[-4] and it exactly tells R which column must remvoer from your data. > my.df[-4] Angel Buffy Firefly 1 7 8 9 2 8 9 9 3 6 4 10 4 9 9 10 51010 10 I may be not right. It is a bit deeper than [] selection. -"DrHorrible" 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 Chunhao Date: Sun, 22 Feb 2009 14:48:10 -0800 From: tony.bre...@googlemail.com To: r-help@r-project.org Subject: [R] Filtering a data frame using a string for colum header Hi all, I was just radomly playing with R and got the following error when trying to filter a data frame using a string: Angel <- c(7,8,6,9,10) Buffy <- c(8,9,4,9,10) Firefly <- c(9,9,10,10,10) DrHorrible <- c(10,9,9,10,10) my.df <- data.frame(Angel, Buffy, Firefly, DrHorrible) my.df["DrHorrible"] DrHorrible 1 10 2 9 3 9 4 10 5 10 my.df[-"DrHorrible"] 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 sessionInfo() 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 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. _ [[elided Hotmail spam]] [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Filtering a data frame using a string for colum header
Cheers guys, yeah, I was using this bit of code as my work around: > filter.col <- which(names(my.df)!="DrHorrible") > my.df[filter.col] 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 > my.df2["DrHorrible"] works, then > my.df2[-"DrHorrible"] 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 wrote: > 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: > > Hi > > > r-help-boun...@r-project.org napsal dne 23.02.2009 03:44:41: > > >> Hi Tony, > > >> I "GUESS" my.df[-"DrHorrible"] does not tell R which column "NUMBER" > > would > >> like to remove. > >> As we know, we could use my.df[-4] and it exactly tells R which column > > must > >> remvoer from your data. > > >> > my.df[-4] > >> Angel Buffy Firefly > >> 1 7 8 9 > >> 2 8 9 9 > >> 3 6 4 10 > >> 4 9 9 10 > >> 5 10 10 10 > > >> I may be not right. > > > It is a bit deeper than [] selection. > > >> -"DrHorrible" > > 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 > > >> Chunhao > > >>> Date: Sun, 22 Feb 2009 14:48:10 -0800 > >>> From: tony.bre...@googlemail.com > >>> To: r-h...@r-project.org > >>> Subject: [R] Filtering a data frame using a string for colum header > > >>> Hi all, > > >>> I was just radomly playing with R and got the following error when > >>> trying to filter a data frame using a string: > > Angel <- c(7,8,6,9,10) > Buffy <- c(8,9,4,9,10) > Firefly <- c(9,9,10,10,10) > DrHorrible <- c(10,9,9,10,10) > my.df <- data.frame(Angel, Buffy, Firefly, DrHorrible) > my.df["DrHorrible"] > >>> DrHorrible > >>> 1 10 > >>> 2 9 > >>> 3 9 > >>> 4 10 > >>> 5 10 > my.df[-"DrHorrible"] > >>> 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 > sessionInfo() > >>> 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 > > >>> __ > >>> r-h...@r-project.org mailing list > >>>https://stat.ethz.ch/mailman/listinfo/r-help > >>> PLEASE do read the posting guide > >http://www.R-project.org/posting-guide.html > >>> and provide commented, minimal, self-contained, reproducible code. > > >> _ > >> [[elided Hotmail spam]] > > >> [[alternative HTML version deleted]] > > >> __ > >> r-h...@r-project.org mailing list > >>https://stat.ethz.ch/mailman/listinfo/r-help > >> PLEASE do read the posting guide > >http://www.R-project.org/posting-guide.html > >> and provide commented, minimal, self-contained, reproducible code. > > > __ > > r-h...@r-project.org mailing list > >https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > -- > Brian D. Ripley, rip...@stats.ox.ac.uk > Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ > University of Oxford, Tel: +44 1865 272861 (self) > 1 South Parks Road, +44 1865 272866 (PA) > Oxford OX1 3TG, UK Fax: +44 1865 272595 > > __ > r-h...@r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.