1

I am reading in temperature and relative humidity. When I execute the command below it gives me an error

data.frame(Temp(F)=temperature, Humidity(%)=humidity)

How can I show the units of measurement as part of the data frame column headings without error? I am using this dataframe in R shiny even if I use back ticks Temp(F) I get Temp.F. whats a solution? I want the column rendered in R shiny to display Temp(F). In R shiny when you run

dfmine <- data.frame(`weird (column) name with %'s and other stuff`= 1:10)

dataframe gets displayed in the UI and the below line does not execute colnames(dfmine) <- "weird (column) name with %'s and other stuff"

Alli
  • 59
  • 3
  • 9
  • You can use `` for using special characters in column name – YOLO Jul 21 '18 at 23:21
  • Related: [Canonicals for R questions on valid/invalid identifiers and “Why do read/make.names modify syntactically invalid identifiers?”](https://meta.stackoverflow.com/questions/370177/canonicals-for-r-questions-on-valid-invalid-identifiers-and-why-do-read-make-na) – smci Jul 22 '18 at 00:42

1 Answers1

8

If you surround the "column name" (different than a true R name) with back-ticks you can get away with accessing columns containing special characters that would otherwise trip up the parser. This means you will need to use those backticks every time you want to refer to that column name. However, the data.frame function won't let you do this unless you explicitly tell it to avoid sanitizing the column names:

> dfmine <- data.frame(`weird (column) name with %'s and other stuff`= 1:10)
> dfmine
   weird..column..name.with...s.and.other.stuff
1                                             1
2                                             2
3                                             3
4                                             4
5                                             5
6                                             6
7                                             7
8                                             8
9                                             9
10                                           10

Failed to set the correct parameter, check.names. Now do it "correctly":

> dfmine <- data.frame(`weird (column) name with %'s and other stuff`= 1:10,
                        check.names=FALSE)
> dfmine$`weird (column) name with %'s and other stuff`
 [1]  1  2  3  4  5  6  7  8  9 10
> dfmine
   weird (column) name with %'s and other stuff
1                                             1
2                                             2
3                                             3
4                                             4
5                                             5
6                                             6
7                                             7
8                                             8
9                                             9
10                                           10

This is generally not a good strategy and there are usually other methods to accomplish whatever it might be that leads you in the direction of this strategy. The other way to get to that point would be to assign a valid name and then reassign using an ordinary character value:

> dfmine <- data.frame(`weird (column) name with %'s and other stuff`= 1:10)
> colnames(dfmine) <- "weird (column) name with %'s and other stuff"
> dfmine
   weird (column) name with %'s and other stuff
1                                             1
2                                             2
3                                             3
4                                             4
5                                             5
6                                             6
7                                             7
8                                             8
9                                             9
10                                           10
IRTFM
  • 258,963
  • 21
  • 364
  • 487