[R] Periods instead of spaces in dataframe names?

Tom Arnold tarnold at smpllc.com
Tue Feb 11 17:39:53 CET 2003


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.

Tom Arnold
Managing Partner, Summit Media Partners LLC
Visit our web site at http://www.summitmediapartners.com




More information about the R-help mailing list