Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between as.character() and as( ,"character") in R

Tags:

r

In the surface they both seem to be doing the same thing. But it seems to be the case that the latter as(,"character") is more powerful.

As an example consider the following:

library(rvest)

temp <- html("http://www.example.com/")
temp <- temp %>% html_node("div p")

str(temp)
#Classes 'XMLInternalElementNode', 'XMLInternalNode', 'XMLAbstractNode' <externalptr> 

as.character(temp) 
#Error in as.vector(x, "character") 
#   cannot coerce type 'externalptr' to vector of type 'character'

Whereas as(temp, "character") gives

#[1] "<p>This domain is established to be used for illustrative examples in documents. You may use this\n    domain in examples without prior coordination or asking for permission.</p>"
like image 616
dimitris_ps Avatar asked Apr 10 '15 16:04

dimitris_ps


People also ask

What does as character do in R?

character() function in R converts a numeric object to a string data type or a character object. If the collection is passed to it as an object, it converts all the elements of the collection to a character or string type.

Is CHR same as string in R?

In R, there's no fundamental distinction between a string and a character. A "string" is just a character variable that contains one or more characters. One thing you should be aware of, however, is the distinction between a scalar character variable, and a vector.

What is the difference between a character and a factor in R?

The main difference is that factors have predefined levels. Thus their value can only be one of those levels or NA. Whereas characters can be anything.

What does CHR mean in R?

The class of an object that holds character strings in R is “character”. A string in R can be created using single quotes or double quotes. chr = 'this is a string' chr = "this is a string"


1 Answers

as.character() is an S3 generic, whereas as() is a function defined in the methods package for S4 generics and methods.

The author of an S3 class has no reason to write an S4 coercion method, so for intance

> as.data.frame(matrix(integer()))
[1] V1
<0 rows> (or 0-length row.names)

but

> as(matrix(integer()), "data.frame")
Error in as(matrix(), "data.frame") : 
  no method or default for coercing "matrix" to "data.frame"

For S4 classes, one (i.e., the package developer) can (and really should) write both S3 and S4 methods for coercion of particular classes; a common paradigm is

as.character.MyClass <- function(x, ...) {}
setAs("MyClass", "character",
      function(from) as.character.MyClass(from))

In your example, the author (of XML) has provided a setAs function without the S3 equivalent, so you get special treatment using as(), but default (i.e., error) when using as.character().

There is no general rule about which is 'more powerful'; it would not be at all surprising to find examples even in base R and the methods package where as.X and as(, "X") behave differently and even in a logically inconsistent way.

In the next release of R (3.2.0) you will be able to say

> methods(class=class(temp))
[1] [[          coerce      html_form   html_node   html_nodes  html_table 
[7] initialize  show        slotsFromS3
see '?methods' for accessing help and source code

where 'coerce' is an indication that there is an S4 method for as(temp, ..."). The actual methods are

> x = methods(class=class(temp))
There were 18 warnings (use warnings() to see them)
> attr(x, "info")
                                                  visible from     generic isS4
coerce,oldClass,S3-method                            TRUE           coerce TRUE
coerce,XMLAbstractDocument,XMLAbstractNode-method    TRUE  XML      coerce TRUE
coerce,XMLDocument,XMLInternalDocument-method        TRUE  XML      coerce TRUE
coerce,XMLInternalDocument,character-method          TRUE  XML      coerce TRUE
coerce,XMLInternalDocument,XMLHashTree-method        TRUE  XML      coerce TRUE
coerce,XMLInternalDocument,XMLInternalNode-method    TRUE  XML      coerce TRUE
coerce,XMLInternalNode,XMLInternalDocument-method    TRUE  XML      coerce TRUE
initialize,oldClass-method                           TRUE       initialize TRUE
show,oldClass-method                                 TRUE             show TRUE
slotsFromS3,oldClass-method                          TRUE      slotsFromS3 TRUE

On the other hand there is

> methods(class="matrix")
 [1] anyDuplicated as.data.frame as.raster     boxplot       coerce       
 [6] determinant   duplicated    edit          head          initialize   
[11] isSymmetric   Math          Math2         Ops           relist       
[16] subset        summary       tail          unique       
see '?methods' for accessing help and source code

where we see methods as.data.frame() and as.raster() available for coercing a matrix.

like image 157
Martin Morgan Avatar answered Nov 15 '22 19:11

Martin Morgan