avneet singh
2005-Apr-26 19:15 UTC
[R] how to add a column with string values to a data frame while still converting them to factors
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.data$Product.Description=="PSX"]="MoldedGraphite" if i do>agm.data$ProdCategoryi 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
avneet singh
2005-Apr-26 20:18 UTC
[R] Re: how to add a column with string values to a data frame while still converting them to factors
i got the first part: agm.data$ProdCategory=as.factor(agm.data$ProdCategory) the second i am still struggling with 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.data$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
Uwe Ligges
2005-Apr-27 06:38 UTC
[R] Re: how to add a column with string values to a data frame while still converting them to factors
avneet singh wrote:> i got the first part: > > agm.data$ProdCategory=as.factor(agm.data$ProdCategory) > > the second i am still struggling withFor the second part, see ?is.na. Uwe Ligges> > 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.data$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 >> > > >
Petr Pikal
2005-Apr-27 07:05 UTC
[R] Re: how to add a column with string values to a data frame while still converting them to factors
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.htmlPetr Pikal petr.pikal at precheza.cz