Hi, Let say I have below matrix mdat <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol = 3, byrow = TRUE, dimnames = list(c("row1", "row2"), c("C.1", "C.2", "C.3"))) Now I can extract a raw by rowname as> mdat['row1', ]C.1 C.2 C.3 1 2 3 However I am also looking for was to extract values as NA when a rowname is supplied which is not existing rownames I should get> mdat['new_raw', ]C.1 C.2 C.3 NA NA NA Current it throws error as default functionality. Is there any way to force R to provide values as NA instead of showing any errore? [[alternative HTML version deleted]]
I don't know a clean way of delivering that result but if you use logical indexing you can get an empty matrix with three columns: str( mdat["nope" %in% rownames(mdat), ] ) ?num[0 , 1:3] ?- attr(*, "dimnames")=List of 2 ? ..$ : NULL ? ..$ : chr [1:3] "C.1" "C.2" "C.3" # it prints thus to the console ?mdat[FALSE,? ] #???? C.1 C.2 C.3 If you have a vector, test_vec of possible matches you could use: mdat[ rownames(mdat) %in% test_vec,? ] **Yet again I am advising you to post in plain text. It's very easy to post in plain text from gmail. Please do so.** -- David. On 8/8/19 8:43 AM, Christofer Bogaso wrote:> Hi, > > Let say I have below matrix > > mdat <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol = 3, byrow = TRUE, > dimnames = list(c("row1", "row2"), > c("C.1", "C.2", "C.3"))) > > > Now I can extract a raw by rowname as > >> mdat['row1', ] > C.1 C.2 C.3 > > 1 2 3 > > > However I am also looking for was to extract values as NA when a > rowname is supplied which is not existing rownames > > I should get > >> mdat['new_raw', ] > C.1 C.2 C.3 > > NA NA NA > > > Current it throws error as default functionality. Is there any way to > force R to provide values as NA instead of showing any errore? > > [[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.
Hi Do you insist to use matrix? If you change matrix to data frame it returns NA as required. mdat C.1 C.2 C.3 row1 1 2 3 row2 11 12 13> mdat["some",]Error in mdat["some", ] : subscript out of bounds> mdatf<-as.data.frame(mdat) > mdatf["some",]C.1 C.2 C.3 NA NA NA NA Cheers Petr> -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of Christofer Bogaso > Sent: Thursday, August 8, 2019 5:44 PM > To: r-help <r-help at r-project.org> > Subject: [R] Extract row as NA with no matching name > > Hi, > > Let say I have below matrix > > mdat <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol = 3, byrow = TRUE, > dimnames = list(c("row1", "row2"), > c("C.1", "C.2", "C.3"))) > > > Now I can extract a raw by rowname as > > > mdat['row1', ] > > C.1 C.2 C.3 > > 1 2 3 > > > However I am also looking for was to extract values as NA when a rowname is > supplied which is not existing rownames > > I should get > > > mdat['new_raw', ] > > C.1 C.2 C.3 > > NA NA NA > > > Current it throws error as default functionality. Is there any way to force R to > provide values as NA instead of showing any errore? > > [[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.Osobn? ?daje: Informace o zpracov?n? a ochran? osobn?ch ?daj? obchodn?ch partner? PRECHEZA a.s. jsou zve?ejn?ny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner?s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/ D?v?rnost: Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a podl?haj? tomuto pr?vn? z?vazn?mu prohl??en? o vylou?en? odpov?dnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/