[R] Using PLYR to apply a custom function to a data frame

arun smartpink111 at yahoo.com
Wed Apr 10 20:47:09 CEST 2013


Hi,
You could try ?within()
set.seed(25)
ALL<- data.frame(q3=sample(15:30,10,replace=TRUE), q1=sample(1:14,10,replace=TRUE))


res<-within(ALL,{lower<-q1- 1.5*(q3-q1);upper<-q3+1.5*(q3-q1)})
 res
#   q3 q1 upper lower
#1  21  5  45.0 -19.0
#2  26  6  56.0 -24.0
#3  17 14  21.5   9.5
#4  29  9  59.0 -21.0
#5  16 10  25.0   1.0
#6  30  3  70.5 -37.5
#7  25  8  50.5 -17.5
#8  20 11  33.5  -2.5
#9  16  7  29.5  -6.5
#10 19 11  31.0  -1.0

#or
libray(plyr)
mutate(ALL,upper=q3+1.5*(q3-q1),lower=q1-1.5*(q3-q1))
#   q3 q1 upper lower
#1  21  5  45.0 -19.0
#2  26  6  56.0 -24.0
#3  17 14  21.5   9.5
#4  29  9  59.0 -21.0
#5  16 10  25.0   1.0
#6  30  3  70.5 -37.5
#7  25  8  50.5 -17.5
#8  20 11  33.5  -2.5
#9  16  7  29.5  -6.5
#10 19 11  31.0  -1.0


A.K.



----- Original Message -----
From: ramoss <ramine.mossadegh at finra.org>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, April 10, 2013 2:29 PM
Subject: [R] Using PLYR to apply a custom function to a data frame

Hello,

I am still struggling w/ the PLYR syntax.  I am trying to build a customized
function to detect outliers in a data frame based on the interquantile
method.  My data frame is called "ALL" &  I am trying to create two new
variables in my data frame:
upper=q3+ 1.5*(q3-q1)   & lower=q1-1.5*(q3-q1).  I already have variables q1
& q3.  How could I create the upper and lower variables using plyr so that
it applies to the whole dataframe.  Thank you for your help.



--
View this message in context: http://r.789695.n4.nabble.com/Using-PLYR-to-apply-a-custom-function-to-a-data-frame-tp4663897.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list