-2

I have a dataframe data[n]

Each n = 2 column list containing x, y values

I need to determine the maximum range of all X's and the the maximum range of all Y's (for charting purpose).

What's the best way to do this? other than looping through each pair.

    X1    Y1      X2      Y2        X3        Y4
    1     10      7       12        30        23
    5     13      9       15        12        4
    7     16      4       3         1         2

I'm looking to calculate :

Min X = 1 Max X = 30

Min Y = 2 Max Y = 23

I don't see any way to do it other than loop through pairs as suggested.

Leehbi
  • 789
  • 10
  • 25
  • 2
    Please clarify what you would like to do. I cannot understand the question. Make a reproducible example (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Just provide a short list of possible input data and expected output. – shadow May 19 '14 at 14:19
  • I'm not quite sure that's what you want but `summary(data)` would give you x and y min and max. – Vincent May 19 '14 at 14:35
  • 1
    Relevant post: [Loop through column pairs and load to an array](http://stackoverflow.com/questions/23738207/loop-through-column-pairs-and-load-to-an-array) – zx8754 May 19 '14 at 14:51
  • My plan is to loop through the columns and use the Range() function for each pair and then check for the max/min. – Leehbi May 19 '14 at 15:12

2 Answers2

2

I'm not sure if this is what you want, but you can try to modify this code to get what you need.

You can write your own function

minmax <- function(df, pattern){
  Min <- min(df[, grepl(pattern, names(df))])
  Max <- max(df[, grepl(pattern, names(df))])  
  return(c(min=Min, max=Max))
}

Now use it!

> minmax(df, "X")
min max 
  1  30 
> minmax(df, "Y")
min max 
  2  23 

Here's another version:

minmax2 <- function(x){
  return(c(min=min(x), max=max(x)))
}

> minmax2(df[, grepl("X", names(df))])
min max 
  1  30 
> minmax2(df[, grepl("Y", names(df))])
min max 
  2  23 
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
1

Try this:

#dummy data
x <- matrix(1:24,ncol=8)

#get range
range(x[,c(seq(1,ncol(x),2))])
range(x[,c(seq(2,ncol(x),2))])
zx8754
  • 52,746
  • 12
  • 114
  • 209