[R] how to add a column with string values to a data frame while still converting them to factors
avneet singh
avneet.chugh at gmail.com
Tue Apr 26 21:15:36 CEST 2005
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
More information about the R-help
mailing list