[R] Blank spaces are replaced by period in read.csv, I want to replace blacks with an underline

peter dalgaard pdalgd at gmail.com
Mon Jun 8 18:04:33 CEST 2015


On 08 Jun 2015, at 17:03 , Sarah Goslee <sarah.goslee at gmail.com> wrote:

> 
> I'd import them with check.names=FALSE, then modify them explicitly:
> 
> 
>> mynames <- c("x y", "x y", "x y", "x y")
>> mynames
> [1] "x y" "x y" "x y" "x y"
>> mynames <- sub(" ", ".", mynames)
>> mynames
> [1] "x.y" "x.y" "x.y" "x.y"
>> mynames <- paste(mynames, seq_along(mynames), sep="_")
>> mynames
> [1] "x.y_1" "x.y_2" "x.y_3" "x.y_4"

Didn't he want x_y.1, not x.y_1? Obviously, just switch "." and "_" for that.

A potential improvement (in case not all columns are "x y") is to replace the last bit with make.unique(mynames).

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list