Dear R-help forum members, Suppose I have a data-frame having two variables and single data for each of them, as described below.
variable_1 variable_2 10 20 I have written a function, say, 'fun' which uses input 10 and 20 and gives me desired result. fun = function(X, Y) { X + Y #( I am just giving an example of process. Actual process is quite different.) } result = fun(variable_1[1], variable_2[1]) # Thus, i should be getting answer 30 which I am storing in say 'ans1.csv' # ____________________________________________________________________ # My problem Suppose instead of having above dataframe having single data for variable 1 and variable 2, I have following data as variable_1 variable_2 10 20 40 30 3 11 I need to run the function 'fun' for each pair of values taken by variable_1 and variable_2 separately. Also, the results (= 30, 70 and 14) obtained for each of above pairs should be stored in different csv files, say "ans1.csv", "ans2.csv" and "ans3.csv" respectively which I can use for further analysis. (In reality each of these output files will consists of 1000 records). As I had mentioned in my earlier mail, I am new to R and I think I should be using apply or sapply or tapply etc., which I have tried but I am not able to proceed further as I am not able to understand it properly. It will be a great help to me if I receive the guidance w.r.t (a) how do I tackle above problem i.e. how do I apply the function to a table so that it will generate different csv files pertaining to pair of values "10 and 20", "40 and 30" and "3 and 11"; (b) I am not that sharp to understand the programming aspects of R taht easily, though I am really keen to learn R, so I will be highly obliged if someone helps me understand with some simple examples as to how "apply", "supply", "tapply", "mapply" etc can be used? I am sure this will go a long way in helping the new learners like me to undesrtand the proper use of these wonderful commands. I hope I am able to put forward my problem properly. Thanking all in advance for the anticipated guidance Amelia Vettori, Auckland [[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.