[R] selecting subsets of data from matrix

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Mon Oct 20 20:22:22 CEST 2003


On 20-Oct-03 Laura Quinn wrote:
> Probably a stupid question, but I don't seem to be able to find the
> answer I'm looking for from any of the R literature. Basically I have
> a matrix with several thousand rows and 20 columns(weather stations)
> of wind direction data.
> 
> I am wanting to extract a matrix which contains data for all columns
> conditional on column 20 having a value of _either_ less than 45 or
> greater than 315. (ie I want to extract a matrix which contains wind
> direction data for all columns {weather stations} when there is a
> prevailing northerly wind for one of the stations).
> 
> I have tried a few different methods of doing this, none with any
> success,
> can anyone please advise?

I'd normally do this kind of thing by setting up an index variable.
Say your matrix is Winds.

  iDir <- (Winds[,20]<45)|(Winds[,20]>315)
  I.want <- Winds[iDir,]

This gives you every row of Winds for which the element in col 20
satisfies the condition.

Of course you can do it in one line with

  I.want <- Winds[(Winds[,20]<45)|(Winds[,20]>315),]

but the advantage of the other is that iDir is there, and much easier to
type, if you need it later.

By the way, if it should happen that there are missing values (coded
as NA), then you may get unwanted results unless you deal with the NAs
explicitly:

  iDir <- ((Winds[,20]<45)|(Winds[,20]>315))&(!is.na(Winds[,20]))

This excludes cases of NA in col 20 (i.e. you only get those cases
where it is definitely known that col20 < 45 or col20 > 315). E.g.:

  x<-(1:20);x[c(3,7,11,15,19)]<-NA; x
    [1]  1  2 NA  4  5  6 NA  8  9 10 NA 12 13 14 NA 16 17 18 NA 20
  x[x>15]
    [1] NA NA NA NA 16 17 18 NA 20

(Not only do you get the x-values satisfying the condition; you get
all the NAs as well).

  x[x>15&(!is.na(x))]
    [1] 16 17 18 20

Ted.




More information about the R-help mailing list