[R] Placing a column name in a variable XXXX

Petr PIKAL petr.pikal at precheza.cz
Mon Aug 29 08:33:48 CEST 2011


Hi

or if Dan prefers data frame (which is also a list)

CInew2 <-function(x,alpha){
data.frame(variable = deparse(substitute(x)), mean=mean(x), alpha = alpha)
}


> CInew2(JOBSTAT, 0.05)
  variable      mean alpha
1  JOBSTAT 0.4567117  0.05

Regards
Petr


> 
> In this case you want to use a 'list' since you want character and
> numerics returned:
> 
> > JOBSTAT<-rnorm(10)
> >
> >
> > # new function that does not return 'x'
> >
> > CInew <-function(x,alpha){
> + list(variable = deparse(substitute(x)), mean=mean(x), alpha = alpha)
> + }
> >
> > CInew(JOBSTAT, 0.05)
> $variable
> [1] "JOBSTAT"
> 
> $mean
> [1] -1.113034
> 
> $alpha
> [1] 0.05
> 
> >
> >
> 
> 
> On Sat, Aug 27, 2011 at 6:58 PM, Dan Abner <dan.abner99 at gmail.com> 
wrote:
> >
> >
> > I want to it return:
> >
> > Variable     Mean        alpha
> > JOBSTAT  -0.1240675  0.05
> >
> > How do I get the function parameter x to equal the name of the object 
that
> > is specified as x as a character string?
> >
> >
> > On Sat, Aug 27, 2011 at 6:41 PM, jim holtman <jholtman at gmail.com> 
wrote:
> >>
> >> The function is doing exactly what you are telling it to do.  You 
have
> >> 'cbind(x, mean(x), alpha)' which is creating a matrix where the first
> >> column is all the values in 'x' and the next two are the recycled
> >> values of mean and alpha.  Is this what you want:
> >>
> >> > JOBSAT<-rnorm(10)
> >> >
> >> > CI<-function(x,alpha){
> >> + cbind(x,mean=mean(x),alpha)
> >> + }
> >> > CI(JOBSAT,.05)
> >>               x       mean alpha
> >>  [1,]  0.8592324 -0.1240675  0.05
> >>  [2,] -0.3128362 -0.1240675  0.05
> >>  [3,] -2.0042218 -0.1240675  0.05
> >>  [4,] -0.4675232 -0.1240675  0.05
> >>  [5,] -0.5776273 -0.1240675  0.05
> >>  [6,]  1.5696650 -0.1240675  0.05
> >>  [7,]  0.8070593 -0.1240675  0.05
> >>  [8,] -0.8257525 -0.1240675  0.05
> >>  [9,]  0.6167636 -0.1240675  0.05
> >> [10,] -0.9054347 -0.1240675  0.05
> >> >
> >> > # new function that does not return 'x'
> >> >
> >> > CInew <-function(x,alpha){
> >> + c(mean=mean(x), alpha = alpha)
> >> + }
> >> > CInew(JOBSAT,.05)
> >>      mean      alpha
> >> -0.1240675  0.0500000
> >>
> >>
> >> On Sat, Aug 27, 2011 at 5:38 PM, Dan Abner <dan.abner99 at gmail.com> 
wrote:
> >> > Hi everyone,
> >> >
> >> > How does one place an object name (in this case a vector name) into
> >> > another
> >> > object (while essentially masking the values of the first object?
> >> >
> >> > For example:
> >> >
> >> >> JOBSAT<-rnorm(40)
> >> >>
> >> >> CI<-function(x,alpha){
> >> > + result<-cbind(x,mean=mean(x),alpha)
> >> > + print(result)
> >> > + }
> >> >> CI(JOBSAT,.05)
> >> >
> >> > I want this to return:
> >> >
> >> > Variable    mean              alpha
> >> > JOBSTAT 0.02844131  0.05
> >> > Instead, I am getting:
> >> >
> >> >                x       mean alpha
> >> >  [1,] -1.07694997 0.02844131  0.05
> >> >  [2,] -1.13910850 0.02844131  0.05
> >> >  [3,] -0.21922026 0.02844131  0.05
> >> >  [4,]  0.38618008 0.02844131  0.05
> >> >  [5,] -1.24303799 0.02844131  0.05
> >> >  [6,] -0.74903752 0.02844131  0.05
> >> >  [7,]  0.96136975 0.02844131  0.05
> >> >  [8,] -0.38891237 0.02844131  0.05
> >> >  [9,] -0.20195871 0.02844131  0.05
> >> > [10,]  0.78104508 0.02844131  0.05
> >> > [11,]  0.87468778 0.02844131  0.05
> >> > [12,] -1.89131480 0.02844131  0.05
> >> >
> >> >
> >> > Thank you!
> >> >
> >> > Dan
> >> >
> >> > [13,]  0.74377795 0.02844131  0.05
> >> > [14,] -0.60006285 0.02844131  0.05
> >> > [15,] -0.76661652 0.02844131  0.05
> >> > [16,]  1.06005258 0.02844131  0.05
> >> > [17,]  0.02173877 0.02844131  0.05
> >> > [18,] -0.36558980 0.02844131  0.05
> >> > [19,] -1.92481588 0.02844131  0.05
> >> > [20,] -0.50337507 0.02844131  0.05
> >> > [21,]  0.82205272 0.02844131  0.05
> >> > [22,]  1.59277572 0.02844131  0.05
> >> > [23,]  0.59965718 0.02844131  0.05
> >> >
> >> >        [[alternative HTML version deleted]]
> >> >
> >> > ______________________________________________
> >> > R-help at r-project.org mailing list
> >> > https://stat.ethz.ch/mailman/listinfo/r-help
> >> > PLEASE do read the posting guide
> >> > http://www.R-project.org/posting-guide.html
> >> > and provide commented, minimal, self-contained, reproducible code.
> >> >
> >>
> >>
> >>
> >> --
> >> Jim Holtman
> >> Data Munger Guru
> >>
> >> What is the problem that you are trying to solve?
> >
> >
> 
> 
> 
> -- 
> Jim Holtman
> Data Munger Guru
> 
> What is the problem that you are trying to solve?
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list