Of course if w9zd9_1, w9zd9_2, w9zd9_3 were elements of a data.frame (or
even a list), you could use indexing,

w9zd9 = data.frame(w9zd9_1 = 1:10, w9zd9_2 = 1:10, w9zd9_3 = -2*1:10)

average = function(numbers=1,  w9zd9){
 if(numbers == 1L) return(w9zd9[ ,1]) # vector case fails with rowMeans
 rowMeans(w9zd9[ ,seq(1, numbers)])
}

average(1, w9zd9)
average(2, w9zd9)
average(3, w9zd9)


HTH,

baptiste

2009/9/14 Don MacQueen <m...@llnl.gov>

> I suppose there are a couple of ways...
>
> if (numbers==1) {
>  distot <-
> } else if (numbers ==2) {
>   distot <-
> } else if (numbers==3) {
>   distot <-
> } else distot <- NA
>
> (letting you fill in the right hand side within each block)
>
> There's also
>
> distot <- switch(numbers,
>  w9zd9_1,
>  (w9zd9_1 + w9zd9_2)/2,
>  (w9zd9_1 + w9zd9_2 + w9zd9_3)/3,
>  NA)
>
> In both cases I added an explicit result in case numbers is not one of the
> expected values.
>
> Generalizing this to an arbitrary value of numbers would be trickier.
>
> -Don
>
>
> At 11:05 AM -0400 9/14/09, Casey Klofstad wrote:
>
>> I'm sure this is easy, but I'm having a hard time figuring out how to
>> recode some data in R.
>>
>> I have a variable "numpeers" which is valued 1, 2, or 3. I also have
>> three other variables called "w9zd9_1," "w9zd9_2," and "w9zd9_3." I
>> want to use these variables to create a new item called "distot."
>>
>> Specifically, here is what I want to do:
>>
>> -if numpeers=1, then distot=w9zd9_1
>>
>> -if numpeers=2, then distot=(w9zd9_1 + w9zd9_2)/2
>>
>> -if numpeers=3, then distot=(w9zd9_1 + w9zd9_2 + w9zd9_3)/3
>>
>> Thanks, in advance, for the help!
>>
>> --
>> Casey A. Klofstad
>> University of Miami
>> Department of Political Science
>> Coral Gables, FL
>>
>> klofs...@gmail.com
>> http://*www.*as.miami.edu/personal/cklofstad/
>>
>> ______________________________________________
>> R-help@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.
>>
>
>
> --
> --------------------------------------
> Don MacQueen
> Environmental Protection Department
> Lawrence Livermore National Laboratory
> Livermore, CA, USA
> 925-423-1062
>
> ______________________________________________
> R-help@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.
>



-- 
_____________________________

Baptiste AuguiƩ

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

http://newton.ex.ac.uk/research/emag
______________________________

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to