Dear useR help,
This is below my toy dataset,
   age married income gender
 young      no    low female
   old     yes    low female
   mid      no   high female
 young     yes   high female
   mid     yes   high female
   mid      no medium female
   old      no medium female
 young     yes medium female
   mid     yes    low   male
   old     yes    low   male
 young      no   high   male
   old      no   high   male
   mid     yes   high   male
 young     yes medium   male
   old     yes medium   male
and I take one random row (young,no,low,female) then I make like this
 
  age       <- c("young","mid","old")
  married   <- c("no","yes")
  income    <- c("low","high","medium")
  gender    <- c("female","male")
then I take one random row again (mid,yes,high,male), now
  age       <- c("mid","young","old")
  married   <- c("yes","no")
  income    <- c("high","low","medium")
  gender    <- c("male","female")
and etc, each I take one random row I put in the first cell in each
own variable.
Is this possible to make a simple function?
Sincerely,
Jan Sabee
Dimitris Rizopoulos
2005-Apr-20  09:26 UTC
[R] Put one random row dataset to first cell variable
maybe something like this could be helpful
f <- function(..., ref){
    lis <- list(...)
    for(i in seq(along=lis)){
        x <- lis[[i]]
        r <- match(ref[i], x)
        lis[[i]] <- x[c(r, seq(along=x)[-r])]
    }
    lis
}
#########
age <- c("young", "mid", "old")
married <- c("no", "yes")
income <- c("low", "high", "medium")
gender <- c("female", "male")
f(age, married, income, gender, ref=c("mid", "yes",
"high", "male"))
f(age, married, income, gender, ref=c("old", "yes",
"medium", "male"))
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
----- Original Message ----- 
From: "Jan Sabee" <jan.sabee at gmail.com>
To: <R-help at stat.math.ethz.ch>
Sent: Wednesday, April 20, 2005 10:46 AM
Subject: [R] Put one random row dataset to first cell variable
> Dear useR help,
> This is below my toy dataset,
>
>   age married income gender
> young      no    low female
>   old     yes    low female
>   mid      no   high female
> young     yes   high female
>   mid     yes   high female
>   mid      no medium female
>   old      no medium female
> young     yes medium female
>   mid     yes    low   male
>   old     yes    low   male
> young      no   high   male
>   old      no   high   male
>   mid     yes   high   male
> young     yes medium   male
>   old     yes medium   male
>
> and I take one random row (young,no,low,female) then I make like 
> this
>
>  age       <- c("young","mid","old")
>  married   <- c("no","yes")
>  income    <- c("low","high","medium")
>  gender    <- c("female","male")
>
> then I take one random row again (mid,yes,high,male), now
>
>  age       <- c("mid","young","old")
>  married   <- c("yes","no")
>  income    <- c("high","low","medium")
>  gender    <- c("male","female")
>
> and etc, each I take one random row I put in the first cell in each
> own variable.
> Is this possible to make a simple function?
>
> Sincerely,
> Jan Sabee
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>
Yes, this is exactly what I want. Many thanks and best regards, Jan Sabee On 4/20/05, Dimitris Rizopoulos <dimitris.rizopoulos at med.kuleuven.ac.be> wrote:> maybe something like this could be helpful > > f <- function(..., ref){ > lis <- list(...) > for(i in seq(along=lis)){ > x <- lis[[i]] > r <- match(ref[i], x) > lis[[i]] <- x[c(r, seq(along=x)[-r])] > } > lis > } > ######### > age <- c("young", "mid", "old") > married <- c("no", "yes") > income <- c("low", "high", "medium") > gender <- c("female", "male") > > f(age, married, income, gender, ref=c("mid", "yes", "high", "male")) > f(age, married, income, gender, ref=c("old", "yes", "medium", "male")) > > Best, > Dimitris > > ---- > Dimitris Rizopoulos > Ph.D. Student > Biostatistical Centre > School of Public Health > Catholic University of Leuven > > Address: Kapucijnenvoer 35, Leuven, Belgium > Tel: +32/16/336899 > Fax: +32/16/337015 > Web: http://www.med.kuleuven.ac.be/biostat/ > http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm > > > ----- Original Message ----- > From: "Jan Sabee" <jan.sabee at gmail.com> > To: <R-help at stat.math.ethz.ch> > Sent: Wednesday, April 20, 2005 10:46 AM > Subject: [R] Put one random row dataset to first cell variable > > > Dear useR help, > > This is below my toy dataset, > > > > age married income gender > > young no low female > > old yes low female > > mid no high female > > young yes high female > > mid yes high female > > mid no medium female > > old no medium female > > young yes medium female > > mid yes low male > > old yes low male > > young no high male > > old no high male > > mid yes high male > > young yes medium male > > old yes medium male > > > > and I take one random row (young,no,low,female) then I make like > > this > > > > age <- c("young","mid","old") > > married <- c("no","yes") > > income <- c("low","high","medium") > > gender <- c("female","male") > > > > then I take one random row again (mid,yes,high,male), now > > > > age <- c("mid","young","old") > > married <- c("yes","no") > > income <- c("high","low","medium") > > gender <- c("male","female") > > > > and etc, each I take one random row I put in the first cell in each > > own variable. > > Is this possible to make a simple function? > > > > Sincerely, > > Jan Sabee > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > > http://www.R-project.org/posting-guide.html > > > >
Jan Sabee wrote:> Dear useR help, > This is below my toy dataset, > > age married income gender > young no low female > old yes low female > mid no high female > young yes high female > mid yes high female > mid no medium female > old no medium female > young yes medium female > mid yes low male > old yes low male > young no high male > old no high male > mid yes high male > young yes medium male > old yes medium male > > and I take one random row (young,no,low,female) then I make like this > > age <- c("young","mid","old") > married <- c("no","yes") > income <- c("low","high","medium") > gender <- c("female","male") > > then I take one random row again (mid,yes,high,male), now > > age <- c("mid","young","old") > married <- c("yes","no") > income <- c("high","low","medium") > gender <- c("male","female") > > and etc, each I take one random row I put in the first cell in each > own variable. > Is this possible to make a simple function? >Assumptions: 1) the object is a data frame. 2) all variables are factors (although I have CMA). 3) you want a list containing vectors of the levels for each value in which the first level is the value in that row. order.levels<-function(dfrow) { nvars<-dim(dfrow)[2] lslist<-sapply(dfrow,levels) for(i in 1:nvars) { if(is.factor(dfrow[,i])) { levelstrings<-levels(dfrow[1,i]) whichlevel<-which(levelstrings==dfrow[1,i]) lslist[[i]]<-c(levelstrings[whichlevel],levelstrings[-whichlevel]) } } return(lslist) } Say your data frame is called toy.df: nrows<-dim(toy.df)[1] order.levels(toy.df[sample(1:nrows,1),]) Whether this is simple is debatable. Jim
Many thanks Jim. This is the others kind of a random row which I can use. Best wishes, Jan Sabee> Assumptions: > 1) the object is a data frame. > 2) all variables are factors (although I have CMA). > 3) you want a list containing vectors of the levels for each value in > which the first level is the value in that row. > > order.levels<-function(dfrow) { > nvars<-dim(dfrow)[2] > lslist<-sapply(dfrow,levels) > for(i in 1:nvars) { > if(is.factor(dfrow[,i])) { > levelstrings<-levels(dfrow[1,i]) > whichlevel<-which(levelstrings==dfrow[1,i]) > lslist[[i]]<-c(levelstrings[whichlevel],levelstrings[-whichlevel]) > } > } > return(lslist) > } > > Say your data frame is called toy.df: > > nrows<-dim(toy.df)[1] > order.levels(toy.df[sample(1:nrows,1),]) > > Whether this is simple is debatable. > > Jim >