Madhavi Bhave
2010-Feb-19 08:20 UTC
[R] How to use same function for diffrent input values
Dear R helpers I have written some function (the actual code I have pasted at the end of mail) like say indiv_rate = function(n, rate_name, rate, rate_rf1, rate_rf2, rate_rf3, rateprob1, rateprob2, rateprob3) { ........some R commands return(data.frame(rate_name, rates = round(rate_data, digits = 4))) } ## INPUT rates = indiv_rate(n = read.csv('number.csv')$n, rate_name = read.csv('rate.csv')$rate_name, rate = read.csv('rate.csv')$rate, rate_rf1 = read.csv('rate_rf.csv')$rate_rf1, rate_rf2 = read.csv('rate_rf.csv')$rate_rf2, rate_rf3 = read.csv('rate_rf.csv')$rate_rf3, rateprob1 = read.csv('rate_probability.csv')$probability1, rateprob2 = read.csv('rate_probability.csv')$probability2, rateprob3 = read.csv('rate_probability.csv')$probability3) ## OUTPUT write.csv(data.frame(rates), 'indiv rates generated.csv', row.names = FALSE) ##_______ end of code ____________________________________________________ So for given rate (which I am deining as rate = read.csv('rate.csv')$rate), I get the desired results. My problem is how do I use this fuction for different 'rates' i.e. for any given rate, I run the function and store the result with the respective rate name? Regards Madhavi (PS - I am avoiding to paste my actual code consuming 60 lines. Still, if somene insists, I can post the same) The INTERNET now has a personality. YOURS! See your Yahoo! Homepage. [[alternative HTML version deleted]]
Barry Rowlingson
2010-Feb-19 08:30 UTC
[R] How to use same function for diffrent input values
On Fri, Feb 19, 2010 at 8:20 AM, Madhavi Bhave <madhavi_bhave at yahoo.com> wrote:> rates = indiv_rate(n = read.csv('number.csv')$n, rate_name = read.csv('rate.csv')$rate_name, rate = read.csv('rate.csv')$rate, rate_rf1 = read.csv('rate_rf.csv')$rate_rf1, > ???????????????????????? rate_rf2 = read.csv('rate_rf.csv')$rate_rf2, rate_rf3 = read.csv('rate_rf.csv')$rate_rf3, > ?????? rateprob1 = read.csv('rate_probability.csv')$probability1, rateprob2 = read.csv('rate_probability.csv')$probability2, rateprob3 = read.csv('rate_probability.csv')$probability3)I'm not sure I understand your question fully, but this example above shows me you have a few other things to learn. Don't do the same thing more than once. Here you are reading the csv files several times. That's horribly inefficient. Do it once, and store the value in a variable: rateprobthing = read.csv('file.csv') and then use 'rateprobthing$whatever' each time. It'll make your code faster and easier to understand. For your question about different rates and whatnot, I don't think it's clear where your different rates come from - is it a whole different set of .csv files? Barry -- blog: http://geospaced.blogspot.com/ web: http://www.maths.lancs.ac.uk/~rowlings web: http://www.rowlingson.com/ twitter: http://twitter.com/geospacedman pics: http://www.flickr.com/photos/spacedman