Please show the data set...
--
View this message in context:
http://r.789695.n4.nabble.com/sorting-dataframe-tp840507p4644857.html
Sent from the R help mailing list archive at Nabble.com.
__
R-help@r-project.org mailing list
h
Works great. I did a couple changes so as to not affect the original
data.frame (and had to add levels back b/c I removed them in the original
read.csv).
a <- data.frame(V1=letters[rep(4:1,2)], V2=1001:1008)
b <- a
levels(b) <- unique(a$V1)
b$V1 <- factor(b$V1,levels=c('c','d','a','b'))
a.sorted
Set the levels of the factor a$V1 to the order
in which you want them to be sorted. E.g.,
> a <- data.frame(V1=letters[rep(4:1,2)], V2=1001:1008)
> a[do.call(order,a[c('V1','V2')]),]
V1 V2
4 a 1004
8 a 1008
3 b 1003
7 b 1007
2 c 1002
6 c 1006
1 d 1001
5 d 1005
That's great - thanks all for your help.--
View this message in context:
http://r.789695.n4.nabble.com/Sorting-dataframe-by-number-of-occurrences-of-factor-tp3485443p3488978.html
Sent from the R help mailing list archive at Nabble.com.
__
R-help@r-proj
to the first two lines of your solutions
df<-data.frame(id=c(1:20),name=c('a','b','b','c','a','d','b','e',
'd','d','c','a','b','a','a','b','f','b','c','g'))
freq <- ave(rep(1, times=nrow(df)), df$name, FUN=sum)
I would add:
df[ sort.list(freq), ]
__
df<-data.frame(id=c(1:20),name=c('a','b','b','c','a','d','b','e','d','d','c','a','b','a','a','b','f','b','c','g'))
freq <- ave(rep(1, times=nrow(df)), df$name, FUN=sum)
rowSums(table(df$name,freq))
--
View this message in context:
http://r.789695.n4.nabble.com/Sorting-dataframe-by-number-of-occ
On Fri, Apr 29, 2011 at 11:17:58PM -0700, adigs wrote:
> Apologies for what's probably quite simple, but I'm having some problems with
> sorting a data frame by the number of occurences of each level of a factor.
>
> df<-data.frame(id=c(1:20),name=c('a','b','b','c','a','d','b','e','d','d','c','a',
- or the orderBy function in the doBy package.
Soren
From: [EMAIL PROTECTED] on behalf of Henrik Bengtsson
Sent: Wed 21-11-2007 11:14
To: Rina Oldager Miehs
Cc: r-help@r-project.org
Subject: Re: [R] sorting dataframe
See order(). -Henrik
On 21/11/2007, Rina
See order(). -Henrik
On 21/11/2007, Rina Oldager Miehs <[EMAIL PROTECTED]> wrote:
> Hello
>
> We have a problem with sorting our dataframe...
> i have tried to write
>
> x <- males[sort(males$index, decreasing=T),]
>
> But that just gives me
>
> > x
> id sex BVgain BVmeat phenogain phenomeat
9 matches
Mail list logo