Re: [R] How to plot Contour with NA in dataframe

From: Earl F. Glynn <efg_at_stowers-institute.org>
Date: Thu 14 Apr 2005 - 00:59:08 EST

"Duncan Murdoch" <murdoch@math.aau.dk> wrote in message news:425CFE4D.8010208@math.aau.dk...
> WeiQiang.Li@seagate.com wrote:

> Your problem isn't the NA values, it's the fact that the contour
> functions want a matrix, and you're passing a data.frame. If you use
> as.matrix on it, it converts to character mode, presumably because your
> last column is entirely missing (so is read as mode logical, not numeric).
>
> Use this massaging on it and the plot will work:
>
> myData <- as.matrix(as.data.frame(lapply(myData,as.numeric)))

This looks unnecessarily complicated here, but appears to be necessary. I normally would try to use only "as.matrix" here but this can fail as shown below, but sometimes can work.

R's "rules" about this conversion seem somewhat arbitrary to me. Example 3, in particular, doesn't make sense to me. Can anyone share some insight on what is going on?


Dummy.txt

1,2,3

4,5,6

# 1. Integers, no missing values; "as.matrix" good enough for conversion

# Results make sense.

> myData <- read.table('Dummy.txt',sep=',')

> typeof(myData)

[1] "list"

> class(myData)

[1] "data.frame"

> myData <- as.matrix(myData)

> myData

  V1 V2 V3

1 1 2 3

2 4 5 6

> typeof(myData)

[1] "integer"

> class(myData)

[1] "matrix"


Dummy.txt

1,,3

4,2.5,6

# 2. Doubles, missing values; "as.matrix" good enough for conversion

# Results make sense.

> myData <- read.table('Dummy.txt',sep=',')

> myData <- as.matrix(myData)

> myData

  V1 V2 V3

1 1 NA 3

2 4 2.5 6

> typeof(myData)

[1] "double"

> class(myData)

[1] "matrix"


Dummy.txt

1,,3

# 3. Drop second row of data from 2 above. Now instead of integers or doubles,

# the type is "character" after using as.matrix?

# Results don't make sense. Why did dropping the second row of data change

# the type to "character" here?

> myData <- read.table('Dummy.txt',sep=',')

> myData <- as.matrix(myData)

> myData

  V1 V2 V3

1 "1" NA "3"

> typeof(myData)

[1] "character"

> class(myData)

[1] "matrix"


Dummy.txt

1,,3

# 4. More complicated solution than 3 above, like what Duncan suggested,

# but this gives expected results

> myData <- read.table('Dummy.txt',sep=',')

> myData <- as.matrix(as.data.frame(lapply(myData,as.numeric)))

> myData

  V1 V2 V3

1 1 NA 3

> typeof(myData)

[1] "double"

> class(myData)

[1] "matrix"


Thanks for any help in clarifying this R subtilty.

efg

--
Earl F. Glynn
Scientific Programmer
Stowers Institute for Medical Research

______________________________________________
R-help@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
Received on Thu Apr 14 01:21:04 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:07 EST