If I understand you right: you have an "overall" order you want to
always keep, but need to get only those elements that show up in your
df while keeping them in the right order. I think this works:
factor(df$name, levels = intersect(OVERALL, df$name))
Michael
On Mon, Jan 16, 2012 at 8:53 AM, mails <mails00000 at gmail.com>
wrote:> Dear list,
>
>
> I am running R code which produces a data.frame with variable rows. For
> plotting purposes I need to relevel
> the column called "names" but since the dimension of the
data.frame can vary
> I do not know how to dynamically
> revel the data.frame.
>
> Here is an example data.frame called df to illustrate what I try to
achieve:
>
>> df <-
data.frame(cbind(c("a","b","c","d","e"),
c(5,6,4,8,9)))
>> colnames(df) <- c("name", "value")
>> df
> ?name value
> 1 ? ?a ? ? 5
> 2 ? ?b ? ? 6
> 3 ? ?c ? ? 4
> 4 ? ?d ? ? 8
> 5 ? ?e ? ? 9
>
> Now the releveling:
> df$name <- factor(df$name,
levels=c("b","d","c","e","a"))
>
> That was easy. But the plotting goes wrong when the data.frame has less
> rows:
>
>> df
> ?name value
> 1 ? ?a ? ? 5
> 4 ? ?d ? ? 8
> 5 ? ?e ? ? 9
>
> Now my script runs the same line again:
> df$name <- factor(df$name,
levels=c("b","d","c","e","a"))
>
> R does not throw any errors, however, lattice does not plot the results
> correctly. So I dynamically create
> a line which does the following: df$name <- factor(df$name,
> levels=c("d","e","a"))
>
>
> Thanks for any advice in advance!
>
> Cheers
>
>
>
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/Relevel-dynamically-tp4299531p4299531.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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.