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

John Sorkin jsorkin at grecc.umaryland.edu
Mon Jun 8 16:24:37 CEST 2015


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 the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. 
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Call
Send SMS
Call from mobile
Add to Skype
You'll need Skype CreditFree via Skype



Confidentiality Statement:
This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. 


More information about the R-help mailing list