[R] Re: how to add a column with string values to a data frame while still converting them to factors

Petr Pikal petr.pikal at precheza.cz
Wed Apr 27 09:05:53 CEST 2005


Hi

On 26 Apr 2005 at 16:18, avneet singh wrote:

> i got the first part:
> 
> agm.data$ProdCategory=as.factor(agm.data$ProdCategory)
> 
> the second i am still struggling with

?is.na

is.na(whatever)

is what you probably want.

Cheers
Petr



> 
> 
> On 4/26/05, avneet singh <avneet.chugh at gmail.com> wrote:
> > I have 2 questions. In essence i am trying to create product
> > categories based on product description and have it as an additional
> > column of my dataframe. some products dont fit any category and i
> > need a list of them. i am having some trouble in this simple (for
> > most) task. Could you please provide suggestions. Thank you.
> > 
> > avneet
> > 
> > Question 1)
> > 
> > I have a data frame
> > 
> > >agm.data=read.xls("agm.xls")
> > 
> > i add a column to it by this and similar statements
> > >agm.data$ProdCategory[agm.data$Product.Description=="PGX"|agm.data$
> > >Product.Description=="PGW"|agm.data$Product.Description=="PS"|agm.d
> > >ata$Product.Description=="PSX"]="Molded
> > Graphite"
> > 
> > if i do
> > >agm.data$ProdCategory
> > 
> > i get:
> > 
> >   [1] "Extruded Graphite"   "Molded Graphite"     "Molded Graphite"
> >    [4] "Extruded Graphite"   "Extruded Graphite"   NA
> >    [7] "Molded Graphite"     "Extruded Graphite"   "Extruded
> >    Graphite"
> >   [10] "Extruded Graphite"   "Extruded Graphite"   "Extruded
> >   Graphite" [13] "Extruded Graphite"   "Porous"             
> >   "Iso-Molded Graphite"
> > .
> > .
> > .
> > .
> > .
> > [1222] "Extruded Graphite"   "Molded Graphite"     "Extruded
> > Graphite" [1225] "Extruded Graphite"   "Extruded Graphite"   NA
> > [1228] "Iso-Molded Graphite" "Extruded Graphite"   NA
> > 
> > if i check the class, i get
> > >is(agm.data$ProdC)
> > [1] "character" "vector"
> > 
> > i want this to have..
> > [1] "factor"   "oldClass"
> > 
> > ..like other columns have
> > 
> > Question 2)some values of agm.data$ProdCategory are NA. i want to
> > find corresponding values of agm.data$Product.Description so i give
> > the following command
> > >agm.data$Product.Description[agm.data$ProdCategory=="NA"]
> > 
> > i get:
> > 
> > [1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
> > <NA> <NA> <NA>
> >  [17] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
> >  <NA>
> > <NA> <NA> <NA>
> > .
> > .
> > .
> > .
> > .
> > .
> > [257] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
> > <NA> <NA> <NA> <NA> [273] <NA> <NA> <NA> <NA> <NA> 165 Levels: 
> > 1.563x24 10x50x60 61x1.25 6x72 890S 8x6 9x7; 10x1.5 AGR ... YBDXX88
> > 
> > --
> > God created man because he was disappointed over the apes. After
> > that he has given up any further experiments ~Mark Twain
> > 
> 
> 
> -- 
> God created man because he was disappointed over the apes. After that
> he has given up any further experiments ~Mark Twain
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list