[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:21:29 CEST 2015


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-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:12}}



More information about the R-help mailing list