[R] How to plot Contour with NA in dataframe
Earl F. Glynn
efg at stowers-institute.org
Wed Apr 13 16:59:08 CEST 2005
"Duncan Murdoch" <murdoch at math.aau.dk> wrote in message
news:425CFE4D.8010208 at math.aau.dk...
> WeiQiang.Li at 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
More information about the R-help
mailing list