David L Carlson
2015-Jun-08 14:21 UTC
[R] Blank spaces are replaced by period in read.csv, I want to replace blacks with an underline
You can use gsub() to change the names:> dat <- data.frame("Var 1"=rnorm(5, 10), "Var 2"=rnorm(5, 15)) > datVar.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)) > datVar_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}}
John Sorkin
2015-Jun-08 14:24 UTC
[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)) > datVar.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)) > datVar_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.
David L Carlson
2015-Jun-08 14:45 UTC
[R] Blank spaces are replaced by period in read.csv, I want to replace blacks with an underline
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"))> datVar_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)) > datVar.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)) > datVar_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}}