[R] merge

Marc Schwartz MSchwartz at mn.rr.com
Sat Jul 9 02:47:16 CEST 2005


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 at 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 at 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 at 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




More information about the R-help mailing list