Re: [R] merge

From: Marc Schwartz <MSchwartz_at_mn.rr.com>
Date: Sat 09 Jul 2005 - 10:47:16 EST

One other option during the import is to set 'strip.white = TRUE' in read.csv(). See ?read.csv for more information. Bear in mind that this will strip both leading and trailing white space in all columns, which may have unintended consequences.

Yet another post-import option, would be to use sub() on specific columns:

> df <- data.frame(A = c("ST ", "ST", "ST ", "ST ", "ST "),

                   B = letters[1:5])

> df
A B 1 ST a 2 ST b 3 ST c 4 ST d 5 ST e

> df$A <- sub('[[:space:]]+$', '', as.character(df$A))
> df

   A B
1 ST a
2 ST b
3 ST c
4 ST d
5 ST e

See ?sub for more information. Be cautious in this case, as you will need to coerce any factors to character vectors as I have done above, and then possibly re-coerce to a factor as you may require.

HTH, Marc Schwartz

On Fri, 2005-07-08 at 20:10 -0400, Gabor Grothendieck wrote:
> trim in package gdata will trim spaces off the beginning and end.
>
>
> On 7/8/05, Ling Jin <ljin@lbl.gov> wrote:
> > Hi all,
> >
> > I have two data frames to merge by a column containing the site names
> > (as characters). However, somehow, one of the site names of one data
> > frame have fixed length, say 8, so the names sometimes have spaces at
> > the end. For example, the site name is "ST", but in one data frame, it
> > is "ST ". Therefore, the merge function won't recognize that "ST"
> > and "ST " are the same, so won't merge accordingly.
> >
> > Is there a easy way to deal with it? Or I should do something during
> > data import? (BTW, I imported the data using read.csv)
> >
> >
> > Thanks!
> >
> > Ling
> >
> > ______________________________________________
> > R-help@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> >
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Jul 09 10:52:44 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:26 EST