# Using iterative functions in R...help!

##### New Member
Hello everyone,

I'm extremely new to using R and have been battling to process some data using an iterative function. I just wondered if anyone might be able to give me a hand with it.

To try to make things clearer (and because I don't know how to explain it any other way) I have a set of data which looks a little like this:

NAME VALUE
Tom 4
Tom 16
Tom 2
Tom 10
Tom 12
Tom 3
Tom 4
Jill 10
Jill 20
Jill 16
Jill 1
Jill 13
Jill 2

For each "person" I am trying to calculate a baseline value produced by taking the mean of their data set +/- 1.5 standard deviations, removing any values outside this range from thier data set, and repeating the process until no values fall outside this range. The final mean will become my baseline.

I have been playing with all kinds of "while" and "if" functions but so far I don't feel like I'm even close. Any advice or links to good easily to follow resources would be very much appreciated.

#### chetan.apa

##### Member
Please provide an sample output that you would like to see. I am not sure what you are trying to is feasible or not..specifically what you mean by "repeating the process until no values fall outside this range"

#### trinker

##### ggplot2orBust
Also when you're posting code, dataframes or computer output it's helpful to wrap this information in code tags by:
1. either clicking the pound (#) sign icon or
2. wrap with [NOPARSE]
Code:
some code
[/NOPARSE]

which produces:
Code:
some code

##### New Member
Hi chatan.apa and tinker,

Thanks for your replies and apologies for the lack of clarity. I think it'll be easier to explain if I calculate one result manually...

So for Tom I would first take the mean +/- 1.5 SD of 4, 16, 2, 10, 12, 3, 4 = 7.28571 +/- 8.0645 = -0.779 to 15.350

I then remove numbers which fall outside this range i.e. 16.

I then recalculate my mean +/- 1.5 SD with the new data set 4, 2, 10, 12, 3, 4 = 5.8333 +/-6.179 = -0.346 to 12.012

At this point none of my new data set falls outside this range (if it did then I would keep repeating the above steps) and so my output for Tom is the mean of the last data set i.e. 5.8333.

I hope that makes things clearer and apologies again for being a complete beginner at this!

#### chetan.apa

##### Member
Hi chatan.apa and tinker,

Thanks for your replies and apologies for the lack of clarity. I think it'll be easier to explain if I calculate one result manually...

So for Tom I would first take the mean +/- 1.5 SD of 4, 16, 2, 10, 12, 3, 4 = 7.28571 +/- 8.0645 = -0.779 to 15.350

I then remove numbers which fall outside this range i.e. 16.

I then recalculate my mean +/- 1.5 SD with the new data set 4, 2, 10, 12, 3, 4 = 5.8333 +/-6.179 = -0.346 to 12.012

At this point none of my new data set falls outside this range (if it did then I would keep repeating the above steps) and so my output for Tom is the mean of the last data set i.e. 5.8333.

I hope that makes things clearer and apologies again for being a complete beginner at this!
Here you go!!

Code:
a<-read.csv("C:/chetan/R/ex1.csv",header=T)

myFun <- function(x)
{
chk<-TRUE
while(chk)
{
avg<-mean(x)
stddev<-sd(x)
lowlimit<-avg - 1.5*stddev
uplimit<-avg + 1.5*stddev
c<-x[x > lowlimit & x < uplimit]
if (length(c)==length(x))
{
chk<-FALSE
}
x<-c
}
return(mean(x))
}

tapply(a$VALUE,a$NAME,myFun)
Output:
Code:
     Jill       Tom
10.333333  5.833333
This took a lot of effort and time! I am also a beginner in R