[R] Help writing function in R....

Tabby dj.lan296 at gmail.com
Sun May 13 06:50:09 CEST 2012


I need help writing function that takes three categorical inputs and returns
a vector of summary statistics based on these inputs. The data set contains
information on retail goods that can be specified by their retail segment,
brand name, and type of good along with its retail price and what it
actually sold for. I need to write a function that will take these inputs
and average, count, and calculate whatever else is needed.

I have set the function up as follows (using made up data):

dataold=data.frame(segment=c("golf","tenis","football","tenis","golf","golf"),
brand=c("x","y","z","y","x","a"),type=c("iron","ball","helmet","shoe","driver","iron"),
retail=c(124,.60,80,75,150,108),actual=c(112,.60,72,75,135,100))

retailsum=funtion(segment,brand,type){

datanew=dataold[which(dataoldsegment='segment' & dataoldbrand='brand' &
dataold$type='type'),c("retail","actaul")]

summary=c(dim(datanew)[1],colMeans(datanew)) return(summary) }

The code inside the function braces works on its own, but once I wrap a
function around it I start getting errors or it will just return 0 counts
and NaN for the means.

Any help would be greatly appreciated. I have very little experience in r,
so I apologize if this is a trivial question, but I have not been able to
find a solution.

Thank you, D


--
View this message in context: http://r.789695.n4.nabble.com/Help-writing-function-in-R-tp4629824.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list