[R] Extract row as NA with no matching name

Christofer Bogaso bog@@o@chr|@to|er @end|ng |rom gm@||@com
Thu Aug 8 17:43:38 CEST 2019


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]]

More information about the R-help mailing list