Hi to all, test <- data.frame("X"=c(1:4),"Y"=c(5:8),"Z"=c(8:11)) test <- test[,-2] Is there a way to specify the col name "Y" to delete instead the number? Kind regards Knut
There might be more elegant ways, but this will do it: test<-test[-match("Y",names(test))] On Feb 23, 2010, at 7:04 AM, Knut Krueger wrote:> Hi to all, > test <- data.frame("X"=c(1:4),"Y"=c(5:8),"Z"=c(8:11)) > test <- test[,-2] > > Is there a way to specify the col name "Y" to delete instead the > number? > > Kind regards Knut > > ______________________________________________ > R-help at 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. >
On Feb 23, 2010, at 6:04 AM, Knut Krueger wrote:> Hi to all, > test <- data.frame("X"=c(1:4),"Y"=c(5:8),"Z"=c(8:11)) > test <- test[,-2] > > Is there a way to specify the col name "Y" to delete instead the > number? >I believe that negative indexing only works with numeric arguments. You could dummy up a negation approach for character vectors with: > test[, !(names(test) %in% c("Y"))] #non-negated logical vector to index X Z 1 1 8 2 2 9 3 3 10 4 4 11 > test[, -grep("Y",names(test))] # negated numeric vector to index X Z 1 1 8 2 2 9 3 3 10 4 4 11 -- David> Kind regards Knut > > ______________________________________________ > R-help at 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.
Hi Knut,> test <- data.frame("X"=c(1:4),"Y"=c(5:8),"Z"=c(8:11)) > test <- test[,-2] > > Is there a way to specify the col name "Y" to delete instead the number?test[,colnames(test)!="Y"] test[,!colnames(test)%in%"Y"] test[,-grep("Y",colnames(test))] bw, Marianne -- Marianne Promberger PhD, King's College London http://promberger.info R version 2.10.1 (2009-12-14) Ubuntu 9.10
test$Y<-NULL test["Z"]<-NULL>>> Todd Ogden <to166 at columbia.edu> 23/02/2010 16:02:01 >>>There might be more elegant ways, but this will do it: test<-test[-match("Y",names(test))] On Feb 23, 2010, at 7:04 AM, Knut Krueger wrote:> Hi to all, > test <- data.frame("X"=c(1:4),"Y"=c(5:8),"Z"=c(8:11)) > test <- test[,-2] > > Is there a way to specify the col name "Y" to delete instead the > number? > > Kind regards Knut > > ______________________________________________ > R-help at 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. >______________________________________________ R-help at 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. ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
One more option: transform(test, Y = NULL) On Tue, Feb 23, 2010 at 9:04 AM, Knut Krueger <rh at krueger-family.de> wrote:> Hi to all, > test <- data.frame("X"=c(1:4),"Y"=c(5:8),"Z"=c(8:11)) > test <- test[,-2] > > Is there a way to specify the col name ?"Y" to delete instead the number? > > Kind regards Knut > > ______________________________________________ > R-help at 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O