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

David L Carlson dcarlson at tamu.edu
Mon Jun 8 16:45:57 CEST 2015


Then using Sarah's suggestion something like?

> dat <- read.table(text="
+ 'Var 1' Var.2
+ 1 6
+ 2 7
+ 3 8
+ 4 9
+ 5 10", header=TRUE, col.names=c("Var_1", "Var.2"))
> dat
  Var_1 Var.2
1     1     6
2     2     7
3     3     8
4     4     9
5     5    10

David C

From: John Sorkin [mailto:jsorkin at grecc.umaryland.edu] 
Sent: Monday, June 8, 2015 9:25 AM
To: David L Carlson
Cc: r-help at r-project.org
Subject: RE: [R] Blank spaces are replaced by period in read.csv, I want to replace blacks with an underline

David,
I appreciate you suggestion, but it won't work for me. I need to replace the space for a period at the time the data are read, not afterward. My variables names have periods I want to keep, if I use your suggestion I will replace the period inserted when the data are read, as well as the period that I want to keep.
Thank you,
John 


John David Sorkin M.D., Ph.D.
Professor of Medicine
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing) 

>>> David L Carlson <dcarlson at tamu.edu> 06/08/15 10:21 AM >>>
You can use gsub() to change the names:

> dat <- data.frame("Var 1"=rnorm(5, 10), "Var 2"=rnorm(5, 15))
> dat
Var.1 Var.2
1 9.627122 14.15376
2 10.741617 16.92937
3 8.492926 15.23767
4 12.226146 15.19834
5 8.829982 14.46957
> names(dat) <- gsub("\\.", "_", names(dat))
> dat
Var_1 Var_2
1 9.627122 14.15376
2 10.741617 16.92937
3 8.492926 15.23767
4 12.226146 15.19834
5 8.829982 14.46957

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352



-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of John Sorkin
Sent: Monday, June 8, 2015 9:16 AM
Cc: r-help at r-project.org
Subject: [R] Blank spaces are replaced by period in read.csv, I want to replace blacks with an underline

I am reading a csv file. The column headers have spaces in them. The spaces are replaced by a period. I want to replace the space by another character (e.g. the underline) rather than the period. Can someone tell me how to accomplish this?Thank you,
John

John David Sorkin M.D., Ph.D.
Professor of Medicine
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing) 


Confidentiality Statement:
This email message, including any attachments, is for th...{{dropped:24}}



More information about the R-help mailing list