Re: [R] Extract row as NA with no matching name

2019-08-08 Thread PIKAL Petr
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  On Behalf Of Christofer Bogaso
> Sent: Thursday, August 8, 2019 5:44 PM
> To: r-help 
> 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@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/

__
R-help@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.


Re: [R] Extract row as NA with no matching name

2019-08-08 Thread David Winsemius
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@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.


__
R-help@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.


[R] Extract row as NA with no matching name

2019-08-08 Thread Christofer Bogaso
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@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.