[R] rearranging rows
kjetil brinchmann halvorsen
kjetil at entelnet.bo
Wed Feb 5 03:45:04 CET 2003
On 5 Feb 2003 at 1:22, Luca De Benedictis wrote:
Well, you want to rearrange according to the mean of factor 1.
Factors are catecorical variables, and doesn't have means, so
I assume you really want to say a numerical variable.
Assuming your data is in a data frame with "Country 1" etc levels of
a factor Country :
attach(your.data.frame)
Something like
means <- tapply(Factor1, Country, mean)
s <- sort(means)
na <- names(s)
ns <- vector(length=length(s))
for (i in 1:n) {
ns[i] <- sum(Country==na[i])
}
names <- rep(na, ns)
your.data.frame <- your.data.frame[names,]
This is assuming that the different countries are contiguous
in the data frame, and is not tested. Surely somebody
have a nicer solution.
Kjetil Halvorsen
>
> Dear all,
> I am working with a matrix structured as follows
>
> Factor 1 Factor2 ...
> Country 1
> Country 1
> Country 2
> Country 2
> ...
>
> Country N
> Country N
>
> and I need to rearrange it according to the following scheme
>
>
> Factor 1 Factor2 ...
> Country 2
> Country 2
> Country N
> Country N
> ...
>
> Country 1
> Country 1
>
> where countries are ordered accordingly to the average value of factor 1
>
> correspondent to each country.
> What is the easy way of doing it?
>
> Many thanks in advance,
>
> Luca
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help
More information about the R-help
mailing list