简体   繁体   English

R中as.character()和as(,“character”)之间有什么区别

[英]What is the difference between as.character() and as( ,“character”) in 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(,"character")更强大。

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 as(temp, "character")给出了

#[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>"

as.character() is an S3 generic, whereas as() is a function defined in the methods package for S4 generics and methods. as.character()是S3泛型,而as()是S4泛型和方法的方法包中定义的函数。

The author of an S3 class has no reason to write an S4 coercion method, so for intance S3类的作者没有理由编写S4强制方法,因此对于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 (ie, the package developer) can (and really should) write both S3 and S4 methods for coercion of particular classes; 对于S4类,一个(即包开发者)可以(并且确实应该)编写用于强制特定类的S3和S4方法; 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 (ie, error) when using as.character() . 在您的示例中,作者(XML)提供了一个没有S3等效的setAs函数,因此您使用as()特殊处理,但使用as.character()时默认(即错误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. 即使在基础R和方法包中找到示例也不足为奇,其中as.X和(,“X”)表现不同,甚至以逻辑上不一致的方式。

In the next release of R (3.2.0) you will be able to say 在R(3.2.0)的下一个版本中,您可以说

> 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 其中'coerce'表示存在一个S4方法as(temp, ...") 。实际的方法是

> 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. 我们看到方法as.data.frame()as.raster()可用于强制矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM