[R] Extract row as NA with no matching name
David Winsemius
dw|n@em|u@ @end|ng |rom comc@@t@net
Thu Aug 8 21:10:37 CEST 2019
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 using 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.
More information about the R-help
mailing list