[R] Periods instead of spaces in dataframe names?

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Tue Feb 11 18:06:03 CET 2003


Tom,

Tom Arnold wrote:
> Basic question:
> when I use names() to extract the name of a dataframe element, why does it
> have "." instead of " " between words?
> 
> Context:
> I'm importing a CSV file of survey results for analysis. I read them like
> this:
> 
> df <- read.csv("surveydata.csv",nrows=40,header=TRUE,
>                na.string=c("N/A",""),comment.char="",strip.white=TRUE)
> 
> To do a summary of the responses to a question, I can now do something like
> this:
> 
> table(df[13])
> 
>  1  2  3  4
> 13 13  4  2
> 
> and can then do a barplot with:
> barplot(table(df[13])))
> 
> Which is fine. But since the first row of my data file includes the questions
> themselves, I want to use those questions as chart titles, like this:
> 
> barplot(table(df[13]),main=names(df[13]))
> 
> This gives an unexpected result: the title has a "." where every space should
> be between words, like this: "How.many.cats.do.you.own"
> 
> I can't figure out why, or how to get spaces instead of "." to show up. I've
> tried using gsub but without success, to substitute " " for "."
> 
> I'm think I'm confused about something fundamental here, and hope someone has
> the patience to enlighten me. I confess to a being a bit vague in my
> understanding of R's handling of arrays, dataframes, and vectors. My
> background is in C programming and I keep looking for a "string"...
> 
> Thank you very much. Please reply or copy me directly if you respond.

You can use check.names = FALSE in your read.csv(...) call. Or you can 
use gsub as in:

names(df2) = gsub("\\.", " ", names(df2))

The "."  must be escaped in gsub if used in the pattern argument.

Regards,
Sundar




More information about the R-help mailing list