[R] field index given name.

William Dunlap wdunlap at tibco.com
Mon Oct 12 18:50:49 CEST 2009


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of tdm
> Sent: Monday, October 12, 2009 12:22 AM
> To: r-help at r-project.org
> Subject: Re: [R] field index given name.
> 
> 
> Thanks - would never have guessed that. I eventually got the 
> following to do
> what I want...
> 
> > colprob <- array(dim=NCOL(iris))
> > for(i in 1:NCOL(iris)){
> + colprob[i]=
> + ifelse(names(iris)[i] == 'Species',1,0.5)
> + }
> > colprob
> [1] 0.5 0.5 0.5 0.5 1.0

I think a more direct way to do what that does
is to make vector with names, where the names
are the column names of 'iris' and then subscript
the vector by name instead of by number.   E.g.,

   > colprob <- rep(0.5, length=ncol(iris)) # initialize probs
   > names(colprob) <- colnames(iris) # initialize names
   > colprob["Species"] <- 1.0  # change prob for "Species"
   > colprob
   Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
            0.5          0.5          0.5          0.5          1.0 
   > colprob["Sepal.Width"] # example prob for "Sepal.Width"
   Sepal.Width 
           0.5

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> 
> 
> 
> 
> Schalk Heunis-2 wrote:
> > 
> > Hi Phil
> > Try the following
> >> which(names(iris)=='Species')
> > [1] 5
> > 
> > HTH
> > Schalk Heunis
> > 
> > On Mon, Oct 12, 2009 at 8:53 AM, tdm <philb at philbrierley.com> wrote:
> > 
> >>
> >> Hi,
> >>
> >> How do I access the index number of a field given I only 
> know the field
> >> name?
> >>
> >> eg - I want to set the probability of the field 'species' 
> higher than the
> >> other fields to use in sampling.
> >>
> >> > colprob <- array(dim=NCOL(iris))
> >> > for(i in 1:NCOL(iris)){colprob[i]=0.5}
> >> > colprob[iris$species] = 1 #this doesn't work
> >> > colprob
> >> [1] 0.5 0.5 0.5 0.5 0.5
> >>
> >>
> >>
> >>
> >>
> >> --
> >> View this message in context:
> >> 
> http://www.nabble.com/field-index-given-name.-tp25851216p25851216.html
> >> Sent from the R help mailing list archive at Nabble.com.
> >>
> >> ______________________________________________
> >> 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.
> >>
> > 
> > 	[[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.
> > 
> > 
> 
> -- 
> View this message in context: 
> http://www.nabble.com/field-index-given-name.-tp25851216p25851466.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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