[R] matrix manipulation question

David Winsemius dwinsemius at comcast.net
Fri Mar 27 18:01:33 CET 2015


On Mar 27, 2015, at 3:41 AM, Stéphane Adamowicz wrote:

> Well, it seems to work with me.
> 

No one is doubting that it worked for you in this instance. What Peter D. was criticizing was the construction :

complete.cases(t(Y))==T

... and it was on two bases that it is "wrong". The first is that `T` is not guaranteed to be TRUE. The second is that the test ==T (or similarly ==TRUE) is completely unnecessary because `complete.cases` returns a logical vector and so that expression is a waste of time.

(The issue of matrix versus dataframe was raised by someone else.)

-- 
David.


> Y <- as.matrix(airquality)
> head(Y, n=8)
>     Ozone Solar.R Wind Temp Month Day
> [1,]    41     190  7.4   67     5   1
> [2,]    36     118  8.0   72     5   2
> [3,]    12     149 12.6   74     5   3
> [4,]    18     313 11.5   62     5   4
> [5,]    NA      NA 14.3   56     5   5
> [6,]    28      NA 14.9   66     5   6
> [7,]    23     299  8.6   65     5   7
> [8,]    19      99 13.8   59     5   8
> 
> Z <- Y[,complete.cases(t(Y))==T]
> 
> head(Z, n=8)
>     Wind Temp Month Day
> [1,]  7.4   67     5   1
> [2,]  8.0   72     5   2
> [3,] 12.6   74     5   3
> [4,] 11.5   62     5   4
> [5,] 14.3   56     5   5
> [6,] 14.9   66     5   6
> [7,]  8.6   65     5   7
> [8,] 13.8   59     5   8
> 
> The columns that contained NA were deleted.
> 
> 
> Le 27 mars 2015 à 10:38, peter dalgaard <pdalgd at gmail.com> a écrit :
> 
>> 
>> On 27 Mar 2015, at 09:58 , Stéphane Adamowicz <stephane.adamowicz at avignon.inra.fr> wrote:
>> 
>>> data_no_NA <- data[, complete.cases(t(data))==T]
>> 
>> Ouch! logical == TRUE is bad, logical == T is worse:
>> 
>> data[, complete.cases(t(data))]
>> 
>> 
>> -- 
>> Peter Dalgaard, Professor,
>> Center for Statistics, Copenhagen Business School
>> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
>> Phone: (+45)38153501
>> Office: A 4.23
>> Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
> 
> 
> 
> _________________________________
> Stéphane Adamowicz
> Inra, centre de recherche Paca, unité PSH
> 228, route de l'aérodrome
> CS 40509
> domaine St Paul, site Agroparc
> 84914 Avignon, cedex 9
> France
> 
> stephane.adamowicz at avignon.inra.fr
> tel.  +33 (0)4 32 72 24 35
> fax. +33 (0)4 32 72 24 32
> do not dial 0 when out of France
> web PSH  : https://www6.paca.inra.fr/psh
> web Inra : http://www.inra.fr/
> _________________________________
> 
> 
> 	[[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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list