Skip to content

how to add a column with string values to a data frame while still converting them to factors

4 messages · avneet singh, Uwe Ligges, PIKAL Petr

#
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
i add a column to it by this and similar statements
Graphite"

if i do
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
[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
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
#
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:

  
    
#
avneet singh wrote:

            
For the second part, see ?is.na.

Uwe Ligges
#
Hi
On 26 Apr 2005 at 16:18, avneet singh wrote:

            
?is.na

is.na(whatever)

is what you probably want.

Cheers
Petr
Petr Pikal
petr.pikal at precheza.cz