[R] Finding ranges on a per-row basis from several objects

Jim Lemon jim at bitwrit.com.au
Fri Feb 5 11:44:36 CET 2010


On 02/05/2010 09:21 PM, Steve Murray wrote:
>
> Dear all,
>
> I'm attemping to find the overall range of values from column 5 of a series of data frames, on a per-row basis, and assign the results to a new object. At present, I'm only able to receive the overall range of all values, whereas I'm intending to get the results of the range for each corresponding row. This is what I have so far:
>
> for (i in seq(nrow(dframe[1])))
>
>      range_models<- rbind(range(c(dframe1[i,5]), range(dframe2[i,5]), range(dframe3[i,5]), range(dframe4[i,5]), range(dframe5[i,5]), range(dframe6[i,5])))
>      }
>
> If it's of any help, the structure of each of the data frames is:
>
>
>
>> str(dframe1)
> 'data.frame':    61538 obs. of  6 variables:
>   $ Latitude    : num  -0.25 -0.25 -0.25 -0.25 -0.25 -0.25 -0.25 -0.25 -0.25 -0.25 ...
>   $ Longitude   : num  -48.8 -49.2 -49.8 -50.2 -50.8 ...
>   $ Runoff_fut  : num  1549 1335 1254 1111 1066 ...
>   $ Runoff_bline: num  1877 1719 1550 1438 1362 ...
>   $ Difference  : num  -328 -383 -296 -327 -297 ...
>   $ PerctDiff   : num  -17.5 -22.3 -19.1 -22.7 -21.8 ...
>
>
> So, just to clarify, I'm trying to find the range of values for row 1 based on dframe1 through to dframe6, then row 2,  row 3 etc etc. and put the range results for each row into a new object of equal row number to the dframe objects.
>
Hi Steve,
If I get the idea, you want to get the ranges of "Difference" across the 
data frames a row at a time. I think this will work:

combdframe<-as.matrix(cbind(dframe1$Difference,dframe2$Difference,
  dframe3$Difference,dframe4$Difference,
  dframe5$Difference,dframe6$Difference))
apply(combdframe,1,range)

You may have to add "na.rm=TRUE" if there are NAs.

Jim



More information about the R-help mailing list