Hi:
And yet another: :)
library(plyr)
top2 <- function(x) {
y <- sort(x[-c(1, 2)], decreasing = TRUE)[1:2]
area <- rep(x[1], 2)
type <- rep(x[2], 2)
data.frame(area, type, value = y, variable = names(y))
}
v <- ldply(apply(df, 1, top2), rbind)
The last line does
here is another way of doing it:
> set.seed(19)
>
> area<-c(rep(1,10),rep(2,10),rep(3,10),rep(4,10),rep(5,10))
> type<-c(rep(1:10,5))
> a<-rnorm(50)
> b<-rnorm(50)
> c<-rnorm(50)
> d<-rnorm(50)
> df<-cbind(area,type,a,b,c,d)
> df1 <- data.frame(df)
> require(reshape2)
> df.melt <- melt(df1, id=c('
Alain -
Here's a reproducible data set:
set.seed(19)
area<-c(rep(1,10),rep(2,10),rep(3,10),rep(4,10),rep(5,10))
type<-c(rep(1:10,5))
a<-rnorm(50)
b<-rnorm(50)
c<-rnorm(50)
d<-rnorm(50)
df<-cbind(area,type,a,b,c,d)
First I'll make a helper function to operate on one
row of the data frame:
Dear R-List,
I have a dataframe
area<-c(rep(1,10),rep(2,10),rep(3,10),rep(4,10),rep(5,10))
type<-c(rep(1:10,5))
a<-rnorm(50)
b<-rnorm(50)
c<-rnorm(50)
d<-rnorm(50)
df<-cbind(area,type,a,b,c,d)
df
area type a b
c d
[1,]11 0.45608192
4 matches
Mail list logo