"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

______________________________________________
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.

Reply via email to