Dear R-experts, How can I remove a certain feature or observation by a part of its name. To be clear, I have a matrix with 766 observations as a rows. The row names are like this 70/556 71.1/280 72.1/556 72.1/343 73.1/390 73.1/556 Now I would like to remove all the rows that contain the text 73.1 Any ideas or suggestion please ? Regards ********************** Ahmed Serag Analytical Chemistry Department Faculty of Pharmacy Al-Azhar University Cairo Egypt [[alternative HTML version deleted]]
Hello, Use grep to get the row indices and then subset with a *negative* index to remove those rows. rn <- scan(what = character(), text = " 70/556 71.1/280 72.1/556 72.1/343 73.1/390 73.1/556 ") mat <- matrix(rnorm(6*6), nrow = 6) row.names(mat) <- rn inx <- grep("73\\.", row.names(mat)) new_mat <- mat[-inx, ] new_mat Hope this helps, Rui Barradas On 5/22/2018 11:48 AM, Ahmed Serag wrote:> Dear R-experts, > > > How can I remove a certain feature or observation by a part of its name. To be clear, I have a matrix with 766 observations as a rows. The row names are like this > > 70/556 > 71.1/280 > 72.1/556 > 72.1/343 > 73.1/390 > 73.1/556 > Now I would like to remove all the rows that contain the text 73.1 > > Any ideas or suggestion please ? > > > Regards > > > > > ********************** > > Ahmed Serag > > Analytical Chemistry Department > > Faculty of Pharmacy > > Al-Azhar University > > Cairo > > Egypt > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >
Hello, Please always cc the list. As for the question, yes, it does. If you want to remove just the ones with exactly 73.1 use the pattern grep("^73\\.1$", etc) Explanation: Beginning of string: ^ End of string: $ Escape special characters: \\ (needed because the period is a special character.) Hope this helps, Rui Barradas On 5/22/2018 12:50 PM, Ahmed Serag wrote:> Thank you Mr. Barradas. The code works great. Unfortunately I have also > some labeles with > > > 173.1 > > 273.1 > > > the grep script remove them also ? > > Any ideas Plz, Thanks again > > > ************************ > > *Ahmed Serag* > > /Analytical Chemistry Department/ > > /Faculty of Pharmacy/ > > /Al-Azhar University/ > > /Cairo/ > > /Egypt/ > > > > ------------------------------------------------------------------------ > *From:* Rui Barradas <ruipbarradas at sapo.pt> > *Sent:* Tuesday, May 22, 2018 1:34 PM > *To:* Ahmed Serag; r-help at r-project.org > *Subject:* Re: [R] remove rows of a matrix by part of its row name > Hello, > > Use grep to get the row indices and then subset with a *negative* index > to remove those rows. > > rn <- scan(what = character(), text = " > 70/556 > 71.1/280 > 72.1/556 > 72.1/343 > 73.1/390 > 73.1/556 > ") > > mat <- matrix(rnorm(6*6), nrow = 6) > row.names(mat) <- rn > > inx <- grep("73\\.", row.names(mat)) > > new_mat <- mat[-inx, ] > new_mat > > > Hope this helps, > > Rui Barradas > > On 5/22/2018 11:48 AM, Ahmed Serag wrote: >> Dear R-experts, >> >> >> How can I remove a certain feature or observation by a part of its name. To be clear, I have a matrix with 766 observations as a rows. The row names are like this >> >> 70/556 >> 71.1/280 >> 72.1/556 >> 72.1/343 >> 73.1/390 >> 73.1/556 >> Now I would like to remove all the rows that contain the text 73.1 >> >> Any ideas or suggestion please ? >> >> >> Regards >> >> >> >> >> ********************** >> >> Ahmed Serag >> >> Analytical Chemistry Department >> >> Faculty of Pharmacy >> >> Al-Azhar University >> >> Cairo >> >> Egypt >> >>??????? [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help > R-help -- Main R Mailing List: Primary help - Homepage - SfS > <https://stat.ethz.ch/mailman/listinfo/r-help> > stat.ethz.ch > The main R mailing list, for announcements about the development of R > and the availability of new code, questions and answers about problems > and solutions using R, enhancements and patches to the source code and > documentation of R, comparison and compatibility with S and S-plus, and > for the posting of nice examples and benchmarks. > > > >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >>
Thanks a lot. The code works great. Regards Ahmed ********************** Ahmed Serag Analytical Chemistry Department Faculty of Pharmacy Al-Azhar University Cairo Egypt ________________________________ From: Rui Barradas <ruipbarradas at sapo.pt> Sent: Tuesday, May 22, 2018 2:16 PM To: Ahmed Serag; R-help at r-project.org Subject: Re: [R] remove rows of a matrix by part of its row name Hello, Please always cc the list. As for the question, yes, it does. If you want to remove just the ones with exactly 73.1 use the pattern grep("^73\\.1$", etc) Explanation: Beginning of string: ^ End of string: $ Escape special characters: \\ (needed because the period is a special character.) Hope this helps, Rui Barradas On 5/22/2018 12:50 PM, Ahmed Serag wrote:> Thank you Mr. Barradas. The code works great. Unfortunately I have also > some labeles with > > > 173.1 > > 273.1 > > > the grep script remove them also ? > > Any ideas Plz, Thanks again > > > ************************ > > *Ahmed Serag* > > /Analytical Chemistry Department/ > > /Faculty of Pharmacy/ > > /Al-Azhar University/ > > /Cairo/ > > /Egypt/ > > > > ------------------------------------------------------------------------ > *From:* Rui Barradas <ruipbarradas at sapo.pt> > *Sent:* Tuesday, May 22, 2018 1:34 PM > *To:* Ahmed Serag; r-help at r-project.org > *Subject:* Re: [R] remove rows of a matrix by part of its row name > Hello, > > Use grep to get the row indices and then subset with a *negative* index > to remove those rows. > > rn <- scan(what = character(), text = " > 70/556 > 71.1/280 > 72.1/556 > 72.1/343 > 73.1/390 > 73.1/556 > ") > > mat <- matrix(rnorm(6*6), nrow = 6) > row.names(mat) <- rn > > inx <- grep("73\\.", row.names(mat)) > > new_mat <- mat[-inx, ] > new_mat > > > Hope this helps, > > Rui Barradas > > On 5/22/2018 11:48 AM, Ahmed Serag wrote: >> Dear R-experts, >> >> >> How can I remove a certain feature or observation by a part of its name. To be clear, I have a matrix with 766 observations as a rows. The row names are like this >> >> 70/556 >> 71.1/280 >> 72.1/556 >> 72.1/343 >> 73.1/390 >> 73.1/556 >> Now I would like to remove all the rows that contain the text 73.1 >> >> Any ideas or suggestion please ? >> >> >> Regards >> >> >> >> >> ********************** >> >> Ahmed Serag >> >> Analytical Chemistry Department >> >> Faculty of Pharmacy >> >> Al-Azhar University >> >> Cairo >> >> Egypt >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-helpthz.ch/mailman/listinfo/r-help> stat.ethz.ch The main R mailing list, for announcements about the development of R and the availability of new code, questions and answers about problems and solutions using R, enhancements and patches to the source code and documentation of R, comparison and compatibility with S and S-plus, and for the posting of nice examples and benchmarks.> <https://stat.ethz.ch/mailman/listinfo/r-help> > stat.ethz.ch > The main R mailing list, for announcements about the development of R > and the availability of new code, questions and answers about problems > and solutions using R, enhancements and patches to the source code and > documentation of R, comparison and compatibility with S and S-plus, and > for the posting of nice examples and benchmarks. > > > >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >>[[alternative HTML version deleted]]
I think it is simpler to use !grepl() instead of -grep() here, since subscripting with logicals works properly when there are no matches. Also, since mat is a matrix, add the argument drop=FALSE so the result is a matrix when all but one rows are omitted. E.g.,> mat <- matrix(1:6, nrow=3, ncol=2,dimnames=list(c("One","Two","Three"),c("A","B")))> str(mat[ -grep("T", rownames(mat)), ]) # bad, not a matrix if only onerow wanted Named int [1:2] 1 4 - attr(*, "names")= chr [1:2] "A" "B"> str(mat[ -grep("X", rownames(mat)), ]) # bad, zero-row matrix if nounwanted rows int[0 , 1:2] - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:2] "A" "B"> str(mat[ !grepl("T", rownames(mat)), , drop=FALSE]) # good, one rowmatrix if only one row wanted int [1, 1:2] 1 4 - attr(*, "dimnames")=List of 2 ..$ : chr "One" ..$ : chr [1:2] "A" "B"> str(mat[ !grepl("X", rownames(mat)), , drop=FALSE]) # good, entire matrixis no unwanted rows int [1:3, 1:2] 1 2 3 4 5 6 - attr(*, "dimnames")=List of 2 ..$ : chr [1:3] "One" "Two" "Three" ..$ : chr [1:2] "A" "B" Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, May 22, 2018 at 4:34 AM, Rui Barradas <ruipbarradas at sapo.pt> wrote:> Hello, > > Use grep to get the row indices and then subset with a *negative* index to > remove those rows. > > rn <- scan(what = character(), text = " > 70/556 > 71.1/280 > 72.1/556 > 72.1/343 > 73.1/390 > 73.1/556 > ") > > mat <- matrix(rnorm(6*6), nrow = 6) > row.names(mat) <- rn > > inx <- grep("73\\.", row.names(mat)) > > new_mat <- mat[-inx, ] > new_mat > > > Hope this helps, > > Rui Barradas > > > On 5/22/2018 11:48 AM, Ahmed Serag wrote: > >> Dear R-experts, >> >> >> How can I remove a certain feature or observation by a part of its name. >> To be clear, I have a matrix with 766 observations as a rows. The row names >> are like this >> >> 70/556 >> 71.1/280 >> 72.1/556 >> 72.1/343 >> 73.1/390 >> 73.1/556 >> Now I would like to remove all the rows that contain the text 73.1 >> >> Any ideas or suggestion please ? >> >> >> Regards >> >> >> >> >> ********************** >> >> Ahmed Serag >> >> Analytical Chemistry Department >> >> Faculty of Pharmacy >> >> Al-Azhar University >> >> Cairo >> >> Egypt >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posti >> ng-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> >> > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posti > ng-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]