[R] Trim trailng space from data.frame factor variables

2007-08-16 Thread Lauri Nikkinen
Hi folks, I would like to trim the trailing spaces in my factor variables using lapply (described in this post by Marc Schwartz: http://tolstoy.newcastle.edu.au/R/e2/help/07/08/22826.html) but the code is not functioning (in this example there is only one factor with trailing spaces): y1 -

Re: [R] Trim trailng space from data.frame factor variables

2007-08-16 Thread Lauri Nikkinen
Thanks Marc! What would be the easiest way to coerce char-variables back to factor-variables? Is there a way to prevent the coercion in d[] - lapply(d, function(x) if (is.factor(x)) sub( +$, , x) else x) ? -Lauri 2007/8/16, Marc Schwartz [EMAIL PROTECTED]: On Thu, 2007-08-16 at 17:54 +0300,

Re: [R] Trim trailng space from data.frame factor variables

2007-08-16 Thread Marc Schwartz
On Thu, 2007-08-16 at 17:54 +0300, Lauri Nikkinen wrote: Hi folks, I would like to trim the trailing spaces in my factor variables using lapply (described in this post by Marc Schwartz: http://tolstoy.newcastle.edu.au/R/e2/help/07/08/22826.html) but the code is not functioning (in this

Re: [R] Trim trailng space from data.frame factor variables

2007-08-16 Thread Marc Schwartz
The easiest way might be to modify the lapply() call as follows: d[] - lapply(d, function(x) if (is.factor(x)) factor(sub( +$, , x)) else x) str(d) 'data.frame': 60 obs. of 3 variables: $ x: Factor w/ 5 levels 1,2,3,4,..: 1 1 1 1 1 1 1 1 1 1 ... $ y: num 7.01 8.33 5.48 6.51 5.61 ... $ f:

Re: [R] Trim trailng space from data.frame factor variables

2007-08-16 Thread Prof Brian Ripley
On Thu, 16 Aug 2007, Marc Schwartz wrote: The easiest way might be to modify the lapply() call as follows: d[] - lapply(d, function(x) if (is.factor(x)) factor(sub( +$, , x)) else x) str(d) 'data.frame': 60 obs. of 3 variables: $ x: Factor w/ 5 levels 1,2,3,4,..: 1 1 1 1 1 1 1 1 1 1

Re: [R] Trim trailng space from data.frame factor variables

2007-08-16 Thread Marc Schwartz
On Thu, 2007-08-16 at 17:52 +0100, Prof Brian Ripley wrote: On Thu, 16 Aug 2007, Marc Schwartz wrote: The easiest way might be to modify the lapply() call as follows: d[] - lapply(d, function(x) if (is.factor(x)) factor(sub( +$, , x)) else x) str(d) 'data.frame': 60 obs. of 3

Re: [R] Trim trailng space from data.frame factor variables

2007-08-16 Thread Phil Spector
If the problem is with the levels of the factor, why not change them directly? d = data.frame(a=1:5, + b=c('one ','two','three ','three ','two')) d$b [1] onetwothree three two Levels: one three two levels(d$b) = sub(' +$','',levels(d$b)) d$b [1] one two three three two