1

Problem:

I am (trying to) construct a data.frame in R, where the column name sometimes includes punctuation characters such as "?".

However, R converts these into "." , for instance:

Code

t= data.frame("a?a" = c(1,2,3), "bee" = c(200,300,400))

t= data.frame("a.a" = c(1,2,3), "bee" = c(200,300,400))

both return

t
  a.a bee
1   1 200
2   2 300
3   3 400

I couldn't get escaping to work either (and would like to avoid it, as I have plenty of strings without special characters).

t= data.frame("a\\?a" = c(1,2,3), "bee" = c(200,300,400))

> t
  a..a bee
1    1 200
2    2 300
3    3 400

Question:

I would like to have characters such as question marks and dots represented in my column names (columns contain results for regular expression searches); in the least, they should be kept distinct.

  • Is there any way to do so?

I felt like this might haven been brought up before but could not find it on here; grateful for any pointers. Thanks!

patrick
  • 4,455
  • 6
  • 44
  • 61
  • I think this question is different from the potential duplicate in that it talks about punctuation rather than whitespace, which is the exclusive focus of the other question. For instance, the pot. dup. did not show up in my search on here. – patrick Aug 21 '17 at 19:42

1 Answers1

4

The column names of a data.frame should be valid variable names for use with functions that support non-standard evaluation and formula interfaces. The data.frame function enforces this requirements by "cleaning" names for you via the make.names() function. If you insist on using "invalid" names, just set the check.names= parameter to FALSE.

data.frame("a?a" = c(1,2,3), "bee" = c(200,300,400), check.names=FALSE)
#   a?a bee
# 1   1 200
# 2   2 300
# 3   3 400
MrFlick
  • 195,160
  • 17
  • 277
  • 295