Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ways to incoporate negative or minus sign in variable name

Tags:

variables

r

R accepts only alphanumeric characters, "dot" and "underscore" in variable names. I had names like tmax_60_days_Dec13-Feb13_mean or tmax_60_days_Dec13-Feb13_tmax:>=:-5. Used such system, so I can parse select sub strings easily and also because I was calculating rolling means and used these conditions themselves as names :o

Until recently, I have got away with it, using get or manually removing the 'apostrophe' which knitr added.

But, when I try to use these variables/column names of data fames in functions like party or randomForests, it backfired. They were not recognised

I can change the colon and hypen to dot or underscore, though I would prefer some other possibility. And the ">=" to "ge" and "<=" to "le". But, how do people code the "negative" or "minus" sign if you want to have it in your variable name or column name of a data frame?

I thought of prefixing the number with "neg" or "minus", but wanted to ask around if there are more elegant ways of doing it or simply to know what other ways people mange it. Thanks

like image 738
Anto Avatar asked Jan 01 '26 04:01

Anto


1 Answers

You can use the comment function:

x <- 1:10
comment(x) <- "this is a comment"
 y <- 1:10
 comment(y) <- "this is another comment"
 xy <- data.frame(x=x,y=y)
 str(xy)
 #----------------    
 'data.frame':  10 obs. of  2 variables:
 $ x: atomic  1 2 3 4 5 6 7 8 9 10
  ..- attr(*, "comment")= chr "this is a comment"
 $ y: atomic  1 2 3 4 5 6 7 8 9 10
  ..- attr(*, "comment")= chr "this is another comment"
 #--------------
 comment(xy$x) <- "prod"
 comment(xy$y) <- "sum"

interpret <- function(x) eval(parse(text=paste0(comment(x) ,"(",quote(x),")") ) )
lapply(xy, interpret)
#-----------------
$x
[1] 3628800

$y
[1] 55

A more expansive response would need a data-object that warrants further testing.

like image 144
IRTFM Avatar answered Jan 05 '26 05:01

IRTFM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!