Hi:

There has to be a better way to do this, but one option is to use pmin() and
pmax().

# Function to apply to pairs of columns:
ordPair <- function(x, y) data.frame(pmin(x, y), pmax(x,y))
u <- with(odataframe, cbind(ordPair(X1, X2), ordPair(X3, X4), ordPair(X5,
X6))
u
  pmin.x..y. pmax.x..y. pmin.x..y. pmax.x..y. pmin.x..y. pmax.x..y.
1          1          2          1          4          1          3
2          1          3          1          3          1          2
3          3          4          1          2          1          2
4          1          2          1          2          1          2
5          2          2          1          3          2          3

The first column is the row names - don't let it confuse you :) Clearly, you
need to rename the columns.

If you need to apply this to a large number of column pairs, I would suggest
doing something like the following:

# Partition columns into odds and evens:
cols1 <- names(odataframe)[(1:ncol(odataframe)) %% 2 == 1]
> cols1
[1] "X1" "X3" "X5"
cols2 <- names(odataframe)[(1:ncol(odataframe)) %% 2 == 0]
> cols2
[1] "X2" "X4" "X6"
# Multiply apply the ordPair() function to successive pairs of columns:
> as.data.frame(do.call(cbind, mapply(ordPair, odataframe[cols1],
odataframe[cols2])))
  V1 V2 V3 V4 V5 V6
1  1  2  1  4  1  3
2  1  3  1  3  1  2
3  3  4  1  2  1  2
4  1  2  1  2  1  2
5  2  2  1  3  2  3

HTH,
Dennis


On Sat, Feb 26, 2011 at 9:07 AM, Umesh Rosyara <rosy...@msu.edu> wrote:

> Hi Jorge and R users
>
> Thank you so much for the responses. You input helped me alot and
> potentially can help me to solve one more problem, but I got error message.
> I am sorry to ask you again but if you can find my problem in quick look
> that will be great. I hope this will not cost alot of your time as this is
> based on your idea.
>
> # Just data
> X1 <- c(1,3,4,2,2)
> X2 <- c(2,1,3,1,2)
> X3 <- c(4,3,2,1,1)
> X4<- c(1,1,1,2,3)
> X5 <- c(3,2,1,1,2)
> X6 <- c(1,1,2,2,3)
> odataframe <- data.frame(X1,X2,X3,X4,X5,X6)
>
> My objective here is sort the value of the pair of variables (X1 and X2, X3
> and X4, X5 and X6 and so on.........)  in such way that the second column
> in
> pair is always higher than the first one (X2 > X1, X4 > X3, X6> X5 and so
> on.......).
>
> Here is my attempt:
> nmrk <- 3
> nvar <- 2*nmrk
> lapply(1:nvar, function(ind){
> # indices for the variables we need
>  a <- seq(1, nvar, by = 2)
>  b <- seq(2, nvar, by = 2)
> # shorting column
> tx[, a[ind]] = ifelse(odataframe[, a[ind]] < odataframe[,b[ind]],
> odataframe[, a[ind]], odataframe[, b[ind]])
> tx[, b[ind]] = ifelse(odataframe[, b[ind]] > dataframe[,a[ind]],
> odataframe[,b[ind]], odataframe[,a[ind]])
> df1 <- transform( odataframe, odataframe[, a[ind]]= tx[, a[ind]],
> odataframe[, b[ind]]= tx[, b[ind]]))
> }
>
> I got the following error:
> Error:
> Error: unexpected '=' in:
> "tx[, b[ind]] = ifelse(odataframe[, b[ind]] > dataframe[,a[ind]],
> odataframe[,b[ind]], odataframe[,a[ind]])
> df1 <- transform( odataframe, odataframe[, a[ind]]="
>
> Thanks;
> Umesh R
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to