Rui,
Yes, that's exactly it, thanks!!
I wouldn't have thought of the "is.atomic".  I was going after a series of
tests for dimension and is.list.


@ R. Michael Weylandt
Good point!
Also, thanks for mentioning stringr.  I always forget about that one.

It's nice that R keeps the base function list short enough that you can
look at it, but it would be nice to have a few more convenience functions
included, especially ones that mirror common functions, like "trim"


On Fri, Aug 3, 2012 at 12:19 PM, Rui Barradas <ruipbarra...@sapo.pt> wrote:

> Hello,
>
> This seems to work.
>
> trim2 <- function(x) {
>     if(is.atomic(x))
>
>         gsub("^[[:space:]]+|[[:space:]**]+$", "", x)
>     else
>         sapply(x, function(y) trim2(y))
> }
>
> # Tests
> trim2(tempobj)
> trim2(tempvec)
> trim2(templist)
> trim2(tempdf)
>
> # Extra test
> templistlist <- list(templist, list(tempobj, tempdf))
> trim2(templistlist)
>
> Note, however, that the df is not returned as a df:
>
> > class(trim2(tempdf))
> [1] "matrix"
>
> Hope this helps,
>
> Rui Barradas
> Em 03-08-2012 17:12, Gene Leynes escreveu:
>
>> 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://www.linkedin.com/in/geneleynes>
>> *
>> <http://goog_598053156>*http:/**/geneorama.com/ <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<https://stat.ethz.ch/mailman/listinfo/r-help>
>> PLEASE do read the posting guide http://www.R-project.org/**
>> posting-guide.html <http://www.R-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>

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

Reply via email to