开发者

Classify or cut dataframe by list of class range and summarize it with ddply

开发者 https://www.devze.com 2023-01-19 15:45 出处:网络
I have question about ddply and subset. I have dataframe df like this : df <- read.table(textConnection(

I have question about ddply and subset.

I have dataframe df like this :

df <- read.table(textConnection(
"   id v_idn v_seed v_time v_pop v_rank v_perco 
    1  15    125648 0      150   1      15      
    2  17    125648 0      120   2      5       
    3  18    125648开发者_Python百科 0      100   3      6       
    4  52    125648 0      25    4      1       

    5  17    125648 10     220   1      5      
    6  15    125648 10     160   2      15       
    7  18    125648 10     110   3      6      
    8  52    125648 10     50    4      1       

    9  56   -11152  0      250   1      17      
    10 15   -11152  0      180   2      15      
    11 18   -11152  0      110   3      6       
    12 22   -11152  0      5     4      14      

    13 56   -11152  10     250   1      17      
    14 15   -11152  10     180   2      15      
    15 22   -11152  10     125   3      14      
    16 18   -11152  10     120   4      6 "), header=TRUE)      

STEP ONE :

I have a list of equal interval with cut_interval like this :

myinterval <- cut_interval(c(15,5,6,1,17,14), length=10)  

So i have two levels here : [0,10) and (10,20]

STEP TWO :

I want each group/class is define by my two levels in v_cut ... like this :

id v_idn v_seed v_time v_pop v_rank v_perco v_cut
1  15    125648 0      150   1      15      (10,20]
2  17    125648 0      120   2      5       [0,10)
3  18    125648 0      100   3      6       [0,10)
4  52    125648 0      25    4      1       [0,10)

5  17    125648 10     220   1      5       [0,10)
6  15    125648 10     160   2      15      (10,20] 
7  18    125648 10     110   3      6       [0,10)
8  52    125648 10     50    4      1       [0,10)

9  56   -11152  0      250   1      17      (10,20]
10 15   -11152  0      180   2      15      (10,20]
11 18   -11152  0      110   3      6       [0,10)
12 22   -11152  0      5     4      14      (10,20]

13 56   -11152  10     250   1      17      (10,20]
14 15   -11152  10     180   2      15      (10,20]
15 22   -11152  10     125   3      14      (10,20]
16 18   -11152  10     120   4      6       [0,10)

STEP 3 :

I want to know the variability of v_rank for x axis, and time for y axis, for each group v_cut, so i need to compute min,mean,max,sd for v_rank value with something like

ddply(df, .(v_cut,v_time), summarize ,mean = mean(v_rank), min = min(v_rank), max = max(v_rank), sd = sd(v_rank))

*RESULT WANTED : *

id  v_time MEAN.v_rank ... v_cut
1   0      2.25            (10,20]
2   0      2.42            [0,10)
3   10     2.25            [0,10)
4   10     2.42            (10,20]

MY PROBLEM

I don't know how to pass step 1 -> step 2 :/

And if it's possible to group by v_cut like my example in step 3 ?

Is there a possibility to make the same things with the "subset" option of ddply ?

One more time, thanks a lot for your help great R guru !

UPDATE 1 :

I have an answer to go step1 to step2 :

df$v_cut <- cut_interval(df$v_perco,n=10)

I'm using plyr, but there are perhaps a better answer in this case ?

Answer to go to step 2 to step 3 ?

UPDATE 2 :

Brandon Bertelsen give me a good answer with melt + cast, but now (to understand) i want to make the same operation with plyr and ddply .. with a different result :

id  v_idn v_time MEAN.v_rank ... v_cut
    1   15   0      2.25            (10,20]
    2   15   10     2.45            (10,20]
    2   17   0      1.52            [0,10)
    2   17   10     2.42            [0,10)
    etc. 

I'm trying with something like this :

r('sumData <- ddply(df, .(v_idn,v_time), summarize,min = min(v_rank),mean =  mean(v_rank), max = max(v_rank), sd=sd(v_rank))')

But i want to have v_cut in my sumData dataframe, how can i do with ddply ? is there an option to make this ? Or merging with initial df and key = v_idn to add column v_cut to sumData is the only good answer ?


You don't really need plyr for this, you can use reshape

## Pull what you need
dfx <- df[c("v_seed", "v_time","v_rank","v_perco")]
## Bring in your cuts
dfx <- data.frame(dfx, ifelse(df$v_perco > 10,"(10,20]", "[0,10)")))
## Rename v_cut
colnames(dfx)[ncol(dfx)] <- "v_cut"       
## Melt it.    
dfx <- melt(dfx, id=c("v_cut", "v_seed", "v_time"))
## Cast it.
dfx <- cast(dfx, v_cut + v_time + v_seed ~ variable, c(mean,min,max,sd))

if you only want the mean, then replace the last line with:

dfx <- cast(dfx, v_cut + v_time + v_seed ~ variable, mean)

type "dfx" and you'll see a data frame with what you asked for.


You're just having a problem with syntax is all:

## Add your cut
df.new <- data.frame(df, ifelse(df$v_perco > 10,"(10,20]", "[0,10)"))
## Rename v_cut
colnames(df.new)[ncol(df.new)] <- "v_cut"   

## Careful here read the note below
df.new <- ddply(df.new, .(v_idn, v_time), function(x) unique(data.frame(
mean =  mean(x$v_rank),
v_cut = x$v_cut
)))

Alternatively:

ddply(df.new, .(v_idn, v_time), summarise, mean=mean(v_rank))

With ".(v_idn, v_time)" you're telling ddply that for each combination of v_idn and v_time, you want it to calculate the mean of v_rank.

0

精彩评论

暂无评论...
验证码 换一张
取 消