Dear R experts, I am a beginner with R and I have a question regarding the output of a function. Basically, I created 2 functions, value1 and value2, that calculate two values, and I would like to print those two values to an output datafile. I tryed the following function: function (x) {write.table(data.frame(value1(x),value2(x)),file=output.txt)} My problem is that I would like to calculate those values for different values of x and to get one line in the output file per value. When I use this function for the second time, it erases the value of the first try, etc. How can I proceed so that I will add a new line to the output at every step instead of deleting the previous line? Thank you very much for your answers! Nicolas -- View this message in context: http://r.789695.n4.nabble.com/Beginner-help-about-output-in-R-tp4648969.html Sent from the R help mailing list archive at Nabble.com.
On Nov 8, 2012, at 9:14 PM, nicolapon <niqo at msn.com> wrote:> Dear R experts, > > I am a beginner with R and I have a question regarding the output of a > function. > > Basically, I created 2 functions, value1 and value2, that calculate two > values, and I would like to print those two values to an output datafile. I > tryed the following function: > > function (x) {write.table(data.frame(value1(x),value2(x)),file=output.txt)} >Type ?write.table and then add append=TRUE to your function call. Michael Weylandt> My problem is that I would like to calculate those values for different > values of x and to get one line in the output file per value. > When I use this function for the second time, it erases the value of the > first try, etc. How can I proceed so that I will add a new line to the > output at every step instead of deleting the previous line? > > Thank you very much for your answers! > > Nicolas > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Beginner-help-about-output-in-R-tp4648969.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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.
Nicolas, Maybe something like this would work for you. Put all of your x values in a list (or a vector, if your x values are scalars). Use sapply to loop through all of your x values, applying the value1() and value2() functions, and saving the results in a data frame. Then write the data frame to a file. allofmyxs <- list(x1=48, x2=21, x3=1.9) mydf <- as.data.frame(t(sapply(allofmyxs, function(x) c(value1(x), value2(x))))) write.table(mydf, file=output.txt) Jean nicolapon <niqo@msn.com> wrote on 11/08/2012 03:14:51 PM:> > Dear R experts, > > I am a beginner with R and I have a question regarding the output of a > function. > > Basically, I created 2 functions, value1 and value2, that calculate two > values, and I would like to print those two values to an outputdatafile. I> tryed the following function: > > function (x){write.table(data.frame(value1(x),value2(x)),file=output.txt)}> > My problem is that I would like to calculate those values for different > values of x and to get one line in the output file per value. > When I use this function for the second time, it erases the value of the > first try, etc. How can I proceed so that I will add a new line to the > output at every step instead of deleting the previous line? > > Thank you very much for your answers! > > Nicolas[[alternative HTML version deleted]]
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Jean V Adams > Sent: Thursday, November 08, 2012 3:52 PM > To: nicolapon > Cc: r-help at r-project.org > Subject: Re: [R] Beginner help about output in R > > Nicolas, > > Maybe something like this would work for you. Put all of your x values > in > a list (or a vector, if your x values are scalars). Use sapply to loop > through all of your x values, applying the value1() and value2() > functions, and saving the results in a data frame. Then write the data > frame to a file. > > allofmyxs <- list(x1=48, x2=21, x3=1.9) > mydf <- as.data.frame(t(sapply(allofmyxs, function(x) c(value1(x), > value2(x))))) > write.table(mydf, file=output.txt) > > Jean > > > > nicolapon <niqo at msn.com> wrote on 11/08/2012 03:14:51 PM: > > > > Dear R experts, > > > > I am a beginner with R and I have a question regarding the output of > a > > function. > > > > Basically, I created 2 functions, value1 and value2, that calculate > two > > values, and I would like to print those two values to an output > datafile. I > > tryed the following function: > > > > function (x) > {write.table(data.frame(value1(x),value2(x)),file=output.txt)} > > > > My problem is that I would like to calculate those values for > different > > values of x and to get one line in the output file per value. > > When I use this function for the second time, it erases the value of > the > > first try, etc. How can I proceed so that I will add a new line to > the > > output at every step instead of deleting the previous line? > > > > Thank you very much for your answers! > > > > Nicolas >That is the approach I was going to suggest. Put your x values into a vector, x, and then something like this. I just made up some functions for computing 2 values, then create an x vector of ten values, then a function for computing and writing the values (trying to follow the OP's original request as closely as possible) # makeup some functions value1 <- function (z) 1/(1+exp(z)) value2 <- function (z) 1/z^.5 # create some data values x <- runif(10) # define write function myWrite <- function(y,file) write.table(data.frame(value1=value1(y),value2=value2(y)),file) # call the function t calculate and write out the values myWrite(x,"c:/tmp/test.txt") Hope this is helpful, Dan Daniel Nordlund Olympia, WA 98504-5204