[R] coerce (list) object to type 'double'

2 views
Skip to first unread message

wendy

unread,
Feb 20, 2010, 12:50:43 PM2/20/10
to r-h...@r-project.org

Dear all,

I am really new to R, and I have problem here. I searched around, but did
not get a solution.

I have a numetrix matrix (20 by 25) saved in .csv. I read it as

>sx<-read.csv("sx.csv",header=F)
Then I try to convert it to numeric using

>sx<-as.numeric(sx)
Error: (list) object cannot be coerced to type 'double'
The class of sx is "data.frame".

I tried to unlist it as,
s<-unlist(sx), but when I look at the dimension,
> dim(s)
NULL

Can anyone tell me how should I load my data as a numeric matrix rather than
"data.frame", or how to convert the data.frame matrix into numeric?

Thank you very much.
Wendy
--
View this message in context: http://n4.nabble.com/coerce-list-object-to-type-double-tp1562988p1562988.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

jim holtman

unread,
Feb 20, 2010, 4:25:44 PM2/20/10
to wendy, r-h...@r-project.org
I assume that what you want is a matrix from the dataframe that was read in:

> x <- read.table(textConnection("1 2 3
+ 4 5 6
+ 7 8 9"))
>
> x
V1 V2 V3
1 1 2 3
2 4 5 6
3 7 8 9
> str(x)
'data.frame': 3 obs. of 3 variables:
$ V1: int 1 4 7
$ V2: int 2 5 8
$ V3: int 3 6 9
> x.n <- as.numeric(x) # not the way to do it


Error: (list) object cannot be coerced to type 'double'

No suitable frames for recover()
> x.n <- as.matrix(x) # convert dataframe to matrix
> str(x.n)
int [1:3, 1:3] 1 4 7 2 5 8 3 6 9
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:3] "V1" "V2" "V3"
> x.n
V1 V2 V3
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
>

> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>


> and provide commented, minimal, self-contained, reproducible code.
>

--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

[[alternative HTML version deleted]]

Reply all
Reply to author
Forward
0 new messages