Michael.Dufault at genzyme.com
2013-Jan-23 18:50 UTC
[R] hyphen replaced by period in header when using read.table
To Whom It May Concern: I have noticed that all of the hyphens ("-") are changed to periods (".") when I try to read.table() and the headers contain "-" I am using R 2.13 on a RedHat system. Here is the situation: I have the following a tab-delimited text file saved as test.txt File1-a.txt File1-b.txt File2-a.txt File2-b.txt 1 1 2 1 1 2 3 2 1 1 2 3 1 2 3 4 When I use:>example <- read.table("test.txt", header = TRUE, sep = "\t")>exampleFile1.a.txt File1.b.txt File2.a.txt File2.b.txt 1 1 2 1 1 2 3 2 1 1 2 3 1 2 3 4 Notice that all of the "-" are changed to "." I read the help(read.table) along with a google search, but I can't find why this is happening. Is there a way to prevent this from happening? Thanks in advance, Mike [[alternative HTML version deleted]]
Sarah Goslee
2013-Jan-23 19:16 UTC
[R] hyphen replaced by period in header when using read.table
Please read ?read.table again, and pay special attention to the check.names argument. A - is not allowed in a column name because it would lead to problems like: mydata$a-b vs mydata$a - b where mydata$a.b has no such confusion. If you must have - instead of ., you can use check.names=FALSE and make sure that you always access the columns using: mydata[, "a-b"] or mydata["a-b"] instead of with the $ shortcut.> mydata <- data.frame(col1 = 1:3, "a-b" = 4:6, check.names=FALSE) > mydatacol1 a-b 1 1 4 2 2 5 3 3 6 Sarah On Wed, Jan 23, 2013 at 1:50 PM, <Michael.Dufault at genzyme.com> wrote:> To Whom It May Concern: > > I have noticed that all of the hyphens ("-") are changed to periods (".") when I try to read.table() and the headers contain "-" > > I am using R 2.13 on a RedHat system. > > Here is the situation: > > I have the following a tab-delimited text file saved as test.txt > > File1-a.txt > > File1-b.txt > > File2-a.txt > > File2-b.txt > > 1 > > 1 > > 2 > > 1 > > 1 > > 2 > > 3 > > 2 > > 1 > > 1 > > 2 > > 3 > > 1 > > 2 > > 3 > > 4 > > > > > When I use: >>example <- read.table("test.txt", header = TRUE, sep = "\t") > >>example > File1.a.txt > > File1.b.txt > > File2.a.txt > > File2.b.txt > > 1 > > 1 > > 2 > > 1 > > 1 > > 2 > > 3 > > 2 > > 1 > > 1 > > 2 > > 3 > > 1 > > 2 > > 3 > > 4 > > > Notice that all of the "-" are changed to "." > > I read the help(read.table) along with a google search, but I can't find why this is happening. > > Is there a way to prevent this from happening? > > Thanks in advance, > Mike >-- Sarah Goslee http://www.functionaldiversity.org