Burt, This is a general problem that I have faced many times, and I'm looking for the best practices for creating an function with a built in iterator. I know that others exist, but I couldn't think of their names off the top of my head... the ones I could remember have the iterator built in at the .Call level.
More message specific responses: "Recursively loop over an object" is a pretty meaningless phrase, > since it depends entirely on the structure of the object. For example, > a character vector is an object, and there is no need for any sort of > recursion to do what you want for it. > When I said "recursively loop over an object" I was referring to the four object types that I gave in my example. I am not trying to be able to handle any type of structure, but at least handle a few types with length > 1 in multiple dimensions. The following regex example trims trailing "spaces" (see ?regex for an > exact definition). > Your regex example is probably nicer (I took my example from an old help response), but the regex is working fine. The problem is that the return is deparsed (or parsed?) text. Running the sub command results in the same results that I was getting > sub("[[:space:]]+$","", templist) [1] "c(\" many spaces \", \" many spaces \")" "c(\" many spaces \", \" many spaces \")" > > Adapt it to whatever structure you like, probably > with apply type functions. > That's the problem! It's not working as I would expect. But note also that (e.g. S3) methods and is.list or is.recursive may > be useful in a more general approach, something like (where deSpace(x) > is a function with the above sub() expression): > I don't mean to be rude, but there is no way that I'm messing around with S3 methods at this time for this problem. That's a whole different way of programming, and I'm not getting on that bandwagon just yet. If that's the only way (which I know it isn't) I'd rather use the one-off methods that are less efficient. I do sincerely appreciate your response, and I'm quite jealous of your URL gene.com. When I was searching for a URL for my personal website I wanted something with my name "gene". The best I could do, even back in 2000, was geneorama! Thank you, Gene Leynes _____________________________________________ *Data Scientist* ***http://www.linkedin.com/in/geneleynes * <http://goog_598053156>*http://geneorama.com/ <http://geneorama.com/%20>* On Fri, Aug 3, 2012 at 12:02 PM, Bert Gunter <gunter.ber...@gene.com> wrote: > "Recursively loop over an object" is a pretty meaningless phrase, > since it depends entirely on the structure of the object. For example, > a character vector is an object, and there is no need for any sort of > recursion to do what you want for it. > > The following regex example trims trailing "spaces" (see ?regex for an > exact definition). Adapt it to whatever structure you like, probably > with apply type functions. > > > x <- c(" ab ","ab \t ","\t\t") > > x > [1] " ab " "ab \t " "\t\t" > > sub("[[:space:]]+$","",x) > [1] " ab" "ab" "" > > But note also that (e.g. S3) methods and is.list or is.recursive may > be useful in a more general approach, something like (where deSpace(x) > is a function with the above sub() expression): > > nospace <- function(x){ > if(is.atomic(x))deSpace(x) > else lapply(x, function(y)Recall(y)) ##?Recall for recursion > } > > Note that this is completely untested, probably fails miserably and > isn't what you want anyway, ... > ;-) > Good luck! > > Cheers, > Bert > > On Fri, Aug 3, 2012 at 9:12 AM, Gene Leynes <gley...@gmail.com> wrote: > > My apologies, I know that this is not a new problem, but I'm not sure how > > to find the answer > > > > I want to recursively loop over an object and trim trailing white space. > > When I use this function on a list of data.frame I get output like this: > > [1] "c(\" many spaces \", \" many spaces \")" "c(\" many spaces > > \", \" many spaces \")" > > > > What should I do to recombine the results? > > If anyone has a good way to search for this type of question, that would > be > > appreciated. I tried rseek.org with "recursive", but after wading > though > > all the rpart references I didn't find something that seemed to help with > > this problem. > > > > Thank you very much. > > > > > > trim <- function(x) { > > if(length(x)>1) sapply(x[1], trim) > > gsub("^[[:space:]]+|[[:space:]]+$", "", x) > > } > > > > tempobj = ' many spaces ' > > tempvec = c(tempobj, tempobj) > > templist = list(tempvec, tempvec) > > tempdf = data.frame(x = tempvec, y = tempvec) > > > > trim(tempobj) > > trim(tempvec) > > trim(templist) > > trim(tempdf) > > > > > > > > > > > > Thank you, > > Gene Leynes > > _____________________________________________ > > *Data Scientist* > > *Mobile: 312-498-7702 > > **http://www.linkedin.com/in/geneleynes > > * > > <http://goog_598053156>*http://geneorama.com/ <http://geneorama.com/%20 > >* > > > > [[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. > > > > -- > > Bert Gunter > Genentech Nonclinical Biostatistics > > Internal Contact Info: > Phone: 467-7374 > Website: > > http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm > [[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.