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. ______________________________________________ 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.