[R] how to write a function that remembers its state across its calls

2010-02-01 Thread Hao Cen
Hi, I wonder how to write a function that remembers its state across its calls. For example, I would like to compute the average of the pass three values the function has seen f(1) # NA f(2) # NA f(3) # 2 f(4) # 3 This would require f to keep track of the values it has seen. In other lan

Re: [R] ff package: ff objects don't reload completely on NFS drives from a different machine

2010-01-25 Thread Hao Cen
Hi Jens, Thanks for the suggestions. I tried closing the ff file on one nfs client and open that file from another client. It doesn't work. Jeff -Original Message- From: Jens Oehlschlägel [mailto:oehl_l...@gmx.de] Sent: Monday, January 25, 2010 5:07 PM To: h...@andrew.cmu.edu Cc: R-help@

[R] ff package: ff objects don't reload completely on NFS drives from a different machine

2010-01-23 Thread Hao Cen
Hi ff users and Jens, I am using the ff package and it has been working great. Recently I noticed an unexpected behavior in the ff package -- when I save an ff matrix on one machine to an NFS drive and load it on another machine from the save NFS drive, I got quote a lot of zeros in the matrix.

[R] "select: bad file descriptor" in the multicore package

2010-01-12 Thread Hao Cen
Hi, I wonder anyone knows what causes the error message "select: bad file descriptor" in the multicore package. This error sometimes occurs and sometimes doesn't. I couldn't find any documentation on this error about this package. thanks Jeff __ R-hel

Re: [R] how to organize a lot of R source files

2010-01-09 Thread Hao Cen
nt is passed to sourceTo()). > > /Henrik > > > On Fri, Jan 8, 2010 at 7:38 AM, Hao Cen wrote: > >> Hi, >> >> >> I wonder what is a better way to organize a lot of R source files. I >> have a lot of utility functions written and store them in several s

[R] how to organize a lot of R source files

2010-01-08 Thread Hao Cen
Hi, I wonder what is a better way to organize a lot of R source files. I have a lot of utility functions written and store them in several source files (e.g util1.R, util2.R,..utilN.R). I also have a master file in which the source command is used to load all the util.R files. When I need to use t

[R] filehash - multiple indices via '[' not allowed when using RDS format

2010-01-02 Thread Hao Cen
Hi, I have been using filehash for a while. It has performed very well. However, recently I found filehash gives an error when I need to do something like db[c("a", "b")] when the db is in RDS format. Does any one know a way to get around that? The code below reproduces the error thanks Jeff f

Re: [R] pass functions and arguments to function

2009-12-29 Thread Hao Cen
n't work here process2(X) } On Tue, December 29, 2009 4:57 pm, Duncan Murdoch wrote: > On 29/12/2009 3:08 PM, Hao Cen wrote: > >> Hi, >> >> >> I wonder how to pass several functions and their arguments as arguments >> to a function. For e

[R] pass functions and arguments to function

2009-12-29 Thread Hao Cen
Hi, I wonder how to pass several functions and their arguments as arguments to a function. For example, the main function is f = function(X ) { process1(X) ... process2(X) } I have a few functions that operate on X, e.g. g1(X, par1), g2(X, par2), g3(X, par3). par1, par2 and par3 are

[R] (no subject)

2009-12-29 Thread Hao Cen
Hi, I wonder how to pass several functions and their arguments as arguments to a function. For example, the main function is f = function(X ) { process(X) ... process(X) } I have a few functions that operate on X, e.g. g1(X, par1), g2(X, par2), g3(X, par3). par1, par2 and par3 are para

[R] how to append new data to saved data on disk efficiently

2009-12-29 Thread Hao Cen
Hi, I currently combine multiple processed data (data frame) into a list and save the list as ".rda" using the save command. When new data come, I load the rda file, process the new data into a data frame, append the data frame to the end of the list, and save the whole list to the disk. The loadi

[R] how to do multiple responses in a linear regression

2009-12-24 Thread Hao Cen
Hi, I have multiple responses y1, y2, .., yn, and would like to do linear regression for each of them with x1, x2, ..., xm. Instead of doing regression n times, it it possible to do it all at once? I tried lm(y1+y2 ~ x1 + x2 + x3) and lm added y1 y2 and then did the regression. thanks Jeff ___

[R] foreach and deparse(substitute(x))

2009-12-11 Thread Hao Cen
Hi, I would like to get the actual object name passed as a parameter of a function and am using deparse(substitute(x)) to do that. It doesn't work when it is used along with the foreach package. Appreciate if any one can give some suggestions on how to make it work with foreach. FUN.aaa <- functi

[R] get the enclosing function name

2009-12-11 Thread Hao Cen
Hi, Is there a way to get the enclosing function name within a function? For example, I would like to have a function getEnclosingFunctionName(). It works like below f = function(){ print(getEnclosingFunctionName()) } f() # will print "f" Thanks Jeff __

[R] find the index of the next largest element in a sorted vector

2009-12-02 Thread Hao Cen
Hi, How can I find the index of the next largest element in a sorted vector if an element is not found. for example, searching 2 in c(0,3,4) would return 1 since 2 is not in the vector and 0 is the next largest element to 2. I tried which and match and neither returns such information. > which(

Re: [R] sort a data frame by a vector

2009-12-02 Thread Hao Cen
"), A2 = c(1,2,3)) >> >> >> dataDF[match(dataDF$A1,vec),] > A1 A2 > 3 C 3 > 2 A 2 > 1 B 1 > > > -Don > > > > At 10:36 PM -0500 12/1/09, Hao Cen wrote: > >> Hi, >> >> >> >> >> I have a a vector and

[R] sort a data frame by a vector

2009-12-01 Thread Hao Cen
Hi, I have a a vector and a data frame with two columns vec = c("C", "A", "B") dataDF = data.frame(A1 = c("B", "A", "C"), A2 = c(1,2,3)) I would like to sort the data frame by column A1 such that the order of elements in A1 is as the same as in vec. After the ordering, the data frame

Re: [R] questions on the ff package

2009-11-26 Thread Hao Cen
Hi Jens, Thanks for your prompt and informative answers. ff is a fabulous package and your suggestions helped me solve my problems at hands. As I need to incrementally increase each of several large matrices (about 1000 rows *1 columns, 1000 matrices) by a row every day. I wonder how efficien

Re: [R] ow to have R automatically print traceback upon errors

2009-11-26 Thread Hao Cen
.@gmail.com] Sent: Monday, November 23, 2009 11:00 PM To: Hao Cen Cc: r-help@r-project.org Subject: Re: [R] ow to have R automatically print traceback upon errors I use this: options(error=utils::recover) and anytime an error occurs in the interactive mode, it will print out the traceback and

[R] questions on the ff package

2009-11-24 Thread Hao Cen
Hi, I have two questions on using the ff package and wonder if anyone who used ff can share some thoughts. I need to save a matrix as a memory-mapped file and load it back later. To save the matrix, I use mat = matrix(1:20, 4, 5) matFF = ff(mat, dim=dim(mat), filename="~/a.mat", overwrite=TRUE,

[R] ow to have R automatically print traceback upon errors

2009-11-23 Thread Hao Cen
Hi, I wonder how to have R automatically print stack trace produced by traceback upon errors during interactive uses. I tried the suggestions on http://old.nabble.com/Automatically-execute-traceback-when-execution-of-script-causes-error--td22368483.html#a22368775 and used options(error = recover)

[R] how to link C code with gsl from R CMD and dyn.load

2009-11-19 Thread Hao Cen
Hi, I am writing a function in C that is meant to be called by R. In the C function, I used a gsl function gsl_stats_mean. The code is as simple as below void gsl(double *m, int *dim){ int r, c; r = dim[0]; c = dim[1]; double mean = gsl_stats_mean(&m[0], 1, r);

Re: [R] extracting the last row of each group in a data frame

2009-11-16 Thread Hao Cen
Thanks to all who helped. These are all great suggestions. Jeff -Original Message- From: Gabor Grothendieck [mailto:ggrothendi...@gmail.com] Sent: Monday, November 16, 2009 6:27 PM To: Hao Cen Cc: r-help@r-project.org Subject: Re: [R] extracting the last row of each group in a data

[R] extracting the last row of each group in a data frame

2009-11-16 Thread Hao Cen
Hi, I would like to extract the last row of each group in a data frame. The data frame is as follows Name Value A 1 A 2 A 3 B 4 B 8 C 2 D 3 I would like to get a data frame as Name Value A 3 B 8 C 2 D 3 Thank you for your suggestions in advance Jeff __

Re: [R] how to pass matrices from C to R effectively

2009-11-12 Thread Hao Cen
ointer and generate the required matrix along the length of the array? If this is not what you meant, would you please give me a small example? Jeff On Thu, November 12, 2009 6:53 pm, Duncan Murdoch wrote: > On 12/11/2009 6:45 PM, Hao Cen wrote: > >> Hi, >> >> >> I

[R] how to pass matrices from C to R effectively

2009-11-12 Thread Hao Cen
Hi, I have C code to produce a lot of matrices to be analyzed. As these matrices are large (> 1000*1) and are a lot (> 1000), I am thinking about how to pass them from C to R effectively. Would you think about the following solution? In R, I create a wrapper function passDataFromCToR = funct

Re: [R] dividing a matrix by positive sum or negative sum depending on the sign

2009-11-12 Thread Hao Cen
unable to compare any of these methods to his > strategy. > > -- > David. > >> >> >>> system.time(replicate(1, t( apply(mat, 1, function(x) >> ifelse( x <0, -x/sum(x[x<0], na.rm=T), x/sum(x[x>0], na.rm=T) ) ) ) ) ) >> user system elapsed

[R] dividing a matrix by positive sum or negative sum depending on the sign

2009-11-11 Thread Hao Cen
Hi, I have a matrix with positive numbers, negative numbers, and NAs. An example of the matrix is as follows -1 -1 2 NA 3 3 -2 -1 1 1 NA -2 I need to compute a scaled version of this matrix. The scaling method is dividing each positive numbers in each row by the sum of positive numbers in that r

Re: [R] save an object by dynamicly created name

2009-11-08 Thread Hao Cen
Hi Henrik, I am using your saveObject/loadObject to handle over 1000 matrices. It worked beautifully. Because I need to load those matrices often for evaluating a few functions on them and those matrices do not fit all in memory at once, is there a way to speed up the loading part? I tried save al

Re: [R] save an object by dynamicly created name

2009-11-02 Thread Hao Cen
Hi Henrik and David, Thank you very much for your suggestions. I found both meet my needs. In the following code, I found save(obj,file=pathname) would save the content into an object called obj. path <- "~/"; dir.create(path); for (i in 1:10) { assign( paste("m", i, sep=""), i:5) filena