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? Thanks in advance!
try: mat[(mat[,20] > 315 | mat[,20] < 45),] At 06:41 PM 10/20/2003 +0100, 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 winddirection>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? > >Thanks in advance! > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help >With best wishes and kind regards I am Sincerely, Corey A. Moffet, Ph.D. Support Scientist University of Idaho Northwest Watershed Research Center 800 Park Blvd, Plaza IV, Suite 105 Boise, ID 83712-7716 Voice: (208) 422-0718 FAX: (208) 334-1502
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)
x[x[,20]<45 | x[,20]>315, ] On Mon, 20 Oct 2003, 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? > > Thanks in advance! > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >-- 620B Bartram Hall bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker Box 118525 (ph) 352-392-5697 Gainesville, FL 32611-8525 (fax) 352-392-3704
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.