[R] deleting column from data frame

David Winsemius dwinsemius at comcast.net
Tue Feb 23 17:02:36 CET 2010


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.



More information about the R-help mailing list