[R] variable name of variable in dataframe

Henrik Bengtsson hb at maths.lth.se
Fri Jul 25 12:57:25 CEST 2003


Assuming 0 <= b < 1 here is one solution:

toy <- function(b=.95){
  toyframe <- data.frame(dummy = c(1, 2))

  # Rename column/field #1
  colnames(toyframe)[1] <- sprintf("lion.%02d", as.integer(100*b)); 

  return(toyframe)
}

Henrik Bengtsson
Lund University

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Tobias Verbeke
> Sent: den 25 juli 2003 12:16
> To: R-help
> Subject: [R] variable name of variable in dataframe
> 
> 
> Dear list,
> 
> If I have this toy function:
> 
> toy <- function(b=.95){
> toyframe <- data.frame(lion.95 = c(1, 2))
> return(toyframe)
> }
> 
> How can I obtain that for any value b, 
> the name of the column becomes "lionb", 
> i.e. lion.95 if b = .95, lion.85 if b = .85 etc.
> knowing that .95 (.85 etc.) may also be
> given as 0.95 (0.85 etc.) but that the
> result should be lion.95 (lion.85 etc.) 
> 
> 
> Thanks in advance,
> 
> Tobias
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list 
> https://www.stat.math.ethz.ch/mailman/listinfo> /r-help
> 
> 
>




More information about the R-help mailing list