Skip to content

real numeric variable transforms into factor:

3 messages · Aldi Kraja, Marc Schwartz

#
On Apr 17, 2009, at 2:52 PM, Aldi Kraja wrote:

            
Looks like you are taking data from SAS perhaps, where the missing  
value indicator is '.'.  In R, where type.convert() is used to  
determine the data types for incoming text data, you get:

 > type.convert(".")
[1] .
Levels: .

That is, a factor.

What you want to do is to set the 'na.strings' argument to  
read.table() to '.' rather than the default 'NA', so that the periods  
are interpreted as missing values and set to NA during import. Thus:

# Create from your data in the clipboard (on OSX)
DF <- read.table(pipe("pbpaste"), header = TRUE, sep = ",", na.strings  
= ".")

 > DF
   ob  x1  y1
1  1 1.1 1/1
2  2 2.1 1/2
3  3 3.2 2/2
4  4  NA 0/0
5  5 4.5 1/1
6  6 5.1 0/0
7  7 6.3 1/1
8  8  NA 1/2

 > str(DF)
'data.frame':	8 obs. of  3 variables:
  $ ob: int  1 2 3 4 5 6 7 8
  $ x1: num  1.1 2.1 3.2 NA 4.5 5.1 6.3 NA
  $ y1: Factor w/ 4 levels "0/0","1/1","1/2",..: 2 3 4 1 2 1 2 3

This is now because:

 > type.convert(".", na.strings = ".")
[1] NA


See ?read.table and ?type.convert for more information.

HTH,

Marc Schwartz
#
Thank you Marc for your detailed and helpful info.

Aldi
Marc Schwartz wrote:
--