[R] selecting subsets of data from matrix

Douglas Bates bates at stat.wisc.edu
Mon Oct 20 20:00:49 CEST 2003


Laura Quinn <laura at env.leeds.ac.uk> writes:

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

Is it a matrix or a data frame?

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

If x is a matrix they you want

  mysubset <- x[x[,20] < 45 | x[,20] > 315,]

If x is a data frame and column 20 is named col20 then you can use

  mysubset <- subset(x, col20 < 45 | col20 >315)




More information about the R-help mailing list