Quantcast

removing columns with negative indexing

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

removing columns with negative indexing

chuck.01
This post has NOT been accepted by the mailing list yet.
Hi,
I am trying to remove columns from the example data "a" given below.
Shouldn't the line below the data work?  Please let me know what I am doing wrong.

a <- dput(new2[1:5,c(6:10)])
structure(list(Last.Name.x = c("Bell", "Procell", "Denney", "Woods",
"Bates"), First.Name.x = c("Amanda", "Jordan", "Thomas", "David",
"Charles"), Lab.01.Bonus..2000589. = c(2L, 2L, 0L, 0L, 0L), Lab.01.Entrance..1999102. = c(10L,
8L, 0L, 0L, 0L), Lab.02.Entrance..1999133. = c(10L, 10L, 6L,
0L, 0L)), .Names = c("Last.Name.x", "First.Name.x", "Lab.01.Bonus..2000589.",
"Lab.01.Entrance..1999102.", "Lab.02.Entrance..1999133."), row.names = c(NA,
5L), class = "data.frame")

# why doesn't this work?
a[, -c("First.Name.x", "Last.Name.x")]




Thanks ahead of time.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: removing columns with negative indexing

arun kirshna
This post has NOT been accepted by the mailing list yet.
This post was updated on .
Hi,
Either use:
a[!grepl("First.Name.x|Last.Name.x",colnames(a))]
#or
a[,!(colnames(a)=="First.Name.x"|colnames(a)=="Last.Name.x")]
#or
 a[,!(colnames(a)%in% c("First.Name.x","Last.Name.x"))]
 # Lab.01.Bonus..2000589. Lab.01.Entrance..1999102. Lab.02.Entrance..1999133.
#1                      2                        10                        10
#2                      2                         8                        10
#3                      0                         0                         6
#4                      0                         0                         0
#5                      0                         0                         0
#or
a[,-match(c("Last.Name.x","First.Name.x"),colnames(a))]
#or
 a[,-which(colnames(a)%in% c("First.Name.x","Last.Name.x"))]
#  Lab.01.Bonus..2000589. Lab.01.Entrance..1999102. Lab.02.Entrance..1999133.
#1                      2                        10                        10
#2                      2                         8                        10
#3                      0                         0                         6
#4                      0                         0                         0
#5                      0                         0                         0

A.K.
Loading...