[R] class<- vs. as()

Spencer Graves spencer.graves at pdf.com
Mon Jul 11 16:30:52 CEST 2005


	  What do you want?  Consider the following:

 > v <- matrix(1:9, 3)
 > class(v[1,1])
[1] "integer"
 > class(as.vector(v))
[1] "integer"
 > v2 <- v
 > dim(v2) <- NULL
 > class(v2)
[1] "integer"

	  spencer graves

stefan.albrecht at allianz.com wrote:

> 
> 
> 
> Dear all,
> 
> I would appreciate a lot, if someone could explain to me in a simple
> way, why the assignment class<- is not always working and one has to
> take as() like in the example below.
> 
> 
>>(v <- matrix(1:9, 3))
> 
>      [,1] [,2] [,3]
> [1,]    1    4    7
> [2,]    2    5    8
> [3,]    3    6    9
> 
>>class(v)
> 
> [1] "matrix"
> 
> 
>>class(v) <- "integer"
>>class(v)
> 
> [1] "matrix"
> 
>>v
> 
>      [,1] [,2] [,3]
> [1,]    1    4    7
> [2,]    2    5    8
> [3,]    3    6    9
> 
> 
>>(vi <- as(v, "integer"))
> 
> [1] 1 2 3 4 5 6 7 8 9
> 
>>class(vi)
> 
> [1] "integer"
> 
> 
> With many thanks,
> 
> Stefan
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at 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

-- 
Spencer Graves, PhD
Senior Development Engineer
PDF Solutions, Inc.
333 West San Carlos Street Suite 700
San Jose, CA 95110, USA

spencer.graves at pdf.com
www.pdf.com <http://www.pdf.com>
Tel:  408-938-4420
Fax: 408-280-7915




More information about the R-help mailing list