Re: [R] Recursive function calls

2012-08-03 Thread R. Michael Weylandt
On Fri, Aug 3, 2012 at 5:41 PM, Gene Leynes wrote: > Ah yes, good point. > > this was easy enough to write, it doesn't lose dimensions, and there's no > unnecessary complexity... unless you're passing in data frames, in which > you'll have to recombine them. > > trim = function(x) gsub("^[[:space:

Re: [R] Recursive function calls

2012-08-03 Thread Gene Leynes
Ah yes, good point. this was easy enough to write, it doesn't lose dimensions, and there's no unnecessary complexity... unless you're passing in data frames, in which you'll have to recombine them. trim = function(x) gsub("^[[:space:]]+|[[:space:]]+$", "", x) trimmer = function(x){ if(is.list(x))

Re: [R] Recursive function calls

2012-08-03 Thread Hadley Wickham
> 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" > sum(sapply(search(), function(x) length(ls(x [1] 2376 Over two thousand

Re: [R] Recursive function calls

2012-08-03 Thread R. Michael Weylandt
On Fri, Aug 3, 2012 at 3:36 PM, Gene Leynes wrote: > 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. > > One other helpful function that I haven't seen anyone mention this far is ? rapply [= r

Re: [R] Recursive function calls

2012-08-03 Thread Gene Leynes
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

Re: [R] Recursive function calls

2012-08-03 Thread Gene Leynes
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 t

Re: [R] Recursive function calls

2012-08-03 Thread Hadley Wickham
On Fri, Aug 3, 2012 at 12:19 PM, Rui Barradas wrote: > Hello, > > This seems to work. > > trim2 <- function(x) { > if(is.atomic(x)) > > gsub("^[[:space:]]+|[[:space:]]+$", "", x) > else > sapply(x, function(y) trim2(y)) > } > Using sapply is a bit dangerous here. Compare:

Re: [R] Recursive function calls

2012-08-03 Thread Rui Barradas
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(temp

Re: [R] Recursive function calls

2012-08-03 Thread R. Michael Weylandt
Note that this is a common enough case that Hadley provides for it with the str_trim() function in his stringr package. Best, Michael On Fri, Aug 3, 2012 at 12:02 PM, Bert Gunter wrote: > "Recursively loop over an object" is a pretty meaningless phrase, > since it depends entirely on the structu

Re: [R] Recursive function calls

2012-08-03 Thread Bert Gunter
"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" (

[R] Recursive function calls

2012-08-03 Thread Gene Leynes
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(\"