Re: [R] Put one random row dataset to first cell variable

From: Jan Sabee <jan.sabee_at_gmail.com>
Date: Wed 20 Apr 2005 - 22:12:09 EST

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
>



R-help@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 Received on Wed Apr 20 22:17:03 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:17 EST