Laura Rodriguez Murillo
2009-Feb-06 15:26 UTC
[R] how to delete specific rows in a data frame where the first column matches any string from a list
Hi, I'm new in the mailing list but I would appreciate if you could help me with this: I have a big matrix from where I need to delete specific rows. The second entry on these rows to delete should match any string within a list (other file with just one column). Thank you so much! Laura
Doran, Harold
2009-Feb-06 15:33 UTC
[R] how to delete specific rows in a data frame where the firstcolumn matches any string from a list
Laura: When posting to this list, it is best to provide a small example. This will facilitate better help. Here is an example of what you want to do.> mat <- matrix(rnorm(10), ncol=2) > mat[,1] [,2] [1,] -0.1418628 -1.4559344 [2,] 0.4022863 1.4727494 [3,] 0.0158310 0.3471318 [4,] 0.3962089 -0.5179074 [5,] 1.8098082 0.7238309 ### Remove rows 2 and 3> (newMat <- mat[c(-2,-3),])[,1] [,2] [1,] -0.1418628 -1.4559344 [2,] 0.3962089 -0.5179074 [3,] 1.8098082 0.7238309 I don't know what you mean about the second part (match a string on a list). So, I didn't do anything with that> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Laura > Rodriguez Murillo > Sent: Friday, February 06, 2009 10:26 AM > To: r-help at r-project.org > Subject: [R] how to delete specific rows in a data frame > where the firstcolumn matches any string from a list > > Hi, > > I'm new in the mailing list but I would appreciate if you > could help me with this: > I have a big matrix from where I need to delete specific > rows. The second entry on these rows to delete should match > any string within a list (other file with just one column). > Thank you so much! > > Laura > > ______________________________________________ > 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. >
Wacek Kusnierczyk
2009-Feb-06 16:00 UTC
[R] how to delete specific rows in a data frame where the first column matches any string from a list
Laura Rodriguez Murillo wrote:> Hi, > > I'm new in the mailing list but I would appreciate if you could help > me with this: > I have a big matrix from where I need to delete specific rows. The > second entry on these rows to delete should match any string within a > list (other file with just one column). > Thank you so much! > >here's one way to do it, illustrated with dummy data: # dummy character matrix data = matrix(replicate(20, paste(sample(letters, 20), collapse="")), ncol=2) # filter out rows where second column does not match 'a' data[-grep('a', d[,2]),] this will work also if your data is actually a data frame: data = as.data.frame(data) data[-grep('a', d[,2]),] note, due to a known issue with grep, this won't work correctly if there are *no* rows that do *not* match the pattern: data[-grep('1', d[,2]),] # should return all of data, but returns an empty matrix with the upcoming version of r, grep will have an additional argument which will make this problem easy to fix: data[grep('a', d[,2], invert=TRUE),] vQ
Laura Rodriguez Murillo
2009-Feb-06 22:29 UTC
[R] how to delete specific rows in a data frame where the first column matches any string from a list
Thank you so much! I finally got it. Laura 2009/2/6 Sebastien Bihorel <Sebastien.Bihorel at cognigencorp.com>:> Hi Laura, > > You might want to read the manual on Data importation and exportation on the > cran webpage http://cran.r-project.org/ > Otherwise, have a look at ?read.table. > > Sebastien >