(Reshape2) Removing lower and upper %0.5 with Melt function

22 views
Skip to first unread message

nserdar

unread,
Sep 24, 2017, 10:24:01 AM9/24/17
to ggplot2
Dear friends,

Panel data set is used in my analysis. Before starting anlaysis,

 I would like to remove lower and upper %0.5 values from data set.

Sample code is below:

molted<-melt(dat,id.vars = c("Company","year","month"),measure.vars = c("Tvol"))

dcast(molted,Company+year+month~variable,fun.aggregate = function(X) dat[X < quantile(X, 0.95),])  ( how to fix this function ?? )


Sample my data:

  V1 Company day month year       TVolume
1:  1   10001    9     1      1986           14160      
2:  2   10001   10     1     1986             0
3:  3   10001   13     1    1986             2805
4:  4   10001   14     1    1986             2070
5:  5   10001   15     1    1986            6000
6:  6   10001   16     1    1986            1500

The data has 1250 company, 30 years, 12 month.


Regards,
Serdar




Corey N

unread,
Sep 25, 2017, 1:06:35 PM9/25/17
to ggplot2
Hello Serder,

I'm most comfortable using the data.table package. And if I'm understanding what you're after, I would do the dcast and then filter like this: 

dat <- as.data.table(dat)

upper_95 <- quantile(dat$TVolume, 0.95)
lower_95 <-  quantile(dat$TVolume, 0.05)

dat <- dat[TVolume %between% c(l_95, u_95), ]

Yet maybe that's not what you're after. 

Cheers,

Corey
Reply all
Reply to author
Forward
0 new messages