English 中文(简体)
对R类物品的类型、方式、类别和类型的全面调查不足
原标题:A comprehensive survey of the types of things in R; mode and class and typeof are insufficient

The language R confuses me. Entities have modes and classes, but even this is insufficient to fully describe the entity.

This answer says

在R,每个物体都有一种模式和等级。

So I did these experiments:

> class(3)
[1] "numeric"
> mode(3)
[1] "numeric"
> typeof(3)
[1] "double"

至今已经够了,但后来我转过了病媒:

> mode(c(1,2))
[1] "numeric"
> class(c(1,2))
[1] "numeric"
> typeof(c(1,2))
[1] "double"

That doesn t make sense. Surely a vector of integers should have a different class, or different mode, than a single integer? My questions are:

  • Does everything in R have (exactly one) class ?
  • Does everything in R have (exactly one) mode ?
  • What, if anything, does typeof tell us?
  • What other information is needed to fully describe an entity? (Where is the vectorness stored, for example?)

<>Update: 显然,字面3只是长1的载体。 没有ars。 但是,我尝试了<代码>mode(“string”)和got “character”,导致我认为,扼杀是一种特性媒介。 但是,如果情况确实如此,那么情况就应该如此,但是这不会错了。 c( h , i )=“hi”

最佳回答

我同意,R的那种制度是令人不快的。 之所以如此,是因为它已经经历了(长时间)的时间......。

请注意,您忽略了一种类似类型的功能:storage.mode,而另一种类似类别的职能,old 类别

因此,modestorage.mode为旧式类型(storage.mode更为准确),tof为新版本,甚至更为准确。

mode(3L)                  # numeric
storage.mode(3L)          # integer
storage.mode(`identical`) # function
storage.mode(`if`)        # function
typeof(`identical`)       # closure
typeof(`if`)              # special

Then class is a whole different story. class is mostly just the class attribute of an object (that s exactly what oldClass returns). But when the class attribute is not set, the class function makes up a class from the object type and the dim attribute.

oldClass(3L) # NULL
class(3L) # integer
class(structure(3L, dim=1)) # array
class(structure(3L, dim=c(1,1))) # matrix
class(list()) # list
class(structure(list(1), dim=1)) # array
class(structure(list(1), dim=c(1,1))) # matrix
class(structure(list(1), dim=1, class= foo )) # foo

Finally, the class can return more than one string, but only if the class attribute is like that. The first string value is then kind of the main class, and the following ones are what it inherits from. The made-up classes are always of length 1.

# Here "A" inherits from "B", which inherits from "C"
class(structure(1, class=LETTERS[1:3])) # "A" "B" "C"

# an ordered factor:
class(ordered(3:1)) # "ordered" "factor"
问题回答

<qu, rel=“nofollow noreferer”>,

library(methods)
library(tibble)
library(purrr)
library(xml2)
library(kable)

setClass("dummy", representation(x="numeric", y="numeric"))

types <- list(
  "logical vector" = logical(),
  "integer vector" = integer(),
  "numeric vector" = numeric(),
  "complex vector" = complex(),
  "character vector" = character(),
  "raw vector" = raw(),
  factor = factor(),
  "logical matrix" = matrix(logical()),
  "numeric matrix" = matrix(numeric()),
  "logical array" = array(logical(8), c(2, 2, 2)),
  "numeric array" = array(numeric(8), c(2, 2, 2)),
  list = list(),
  pairlist = .Options,
  "data frame" = data.frame(),
  "closure function" = identity,
  "builtin function" = `+`,
  "special function" = `if`,
  environment = new.env(),
  null = NULL,
  formula = y ~ x,
  expression = expression(),
  call = call("identity"),
  name = as.name("x"),
  "paren in expression" = expression((1))[[1]],
  "brace in expression" = expression({1})[[1]],
  "S3 lm object" = lm(dist ~ speed, cars),
  "S4 dummy object" = new("dummy", x = 1:10, y = rnorm(10)),
  "external pointer" = read_xml("<foo><bar /></foo>")$node
)

type_info <- imap_dfr(
  types,
  function(x, nm)
  {
    tibble(
      "spoken type" = nm,
      class = class(x), 
      typeof = typeof(x),
      mode  = mode(x),
      storage.mode = storage.mode(x)
    )
  }
)

knitr::kable(type_info)

这里的产出:

|spoken type         |class       |typeof      |mode        |storage.mode |
|:-------------------|:-----------|:-----------|:-----------|:------------|
|logical vector      |logical     |logical     |logical     |logical      |
|integer vector      |integer     |integer     |numeric     |integer      |
|numeric vector      |numeric     |double      |numeric     |double       |
|complex vector      |complex     |complex     |complex     |complex      |
|character vector    |character   |character   |character   |character    |
|raw vector          |raw         |raw         |raw         |raw          |
|factor              |factor      |integer     |numeric     |integer      |
|logical matrix      |matrix      |logical     |logical     |logical      |
|logical matrix      |array       |logical     |logical     |logical      |
|numeric matrix      |matrix      |double      |numeric     |double       |
|numeric matrix      |array       |double      |numeric     |double       |
|logical array       |array       |logical     |logical     |logical      |
|numeric array       |array       |double      |numeric     |double       |
|list                |list        |list        |list        |list         |
|pairlist            |pairlist    |pairlist    |pairlist    |pairlist     |
|data frame          |data.frame  |list        |list        |list         |
|closure function    |function    |closure     |function    |function     |
|builtin function    |function    |builtin     |function    |function     |
|special function    |function    |special     |function    |function     |
|environment         |environment |environment |environment |environment  |
|null                |NULL        |NULL        |NULL        |NULL         |
|formula             |formula     |language    |call        |language     |
|expression          |expression  |expression  |expression  |expression   |
|call                |call        |language    |call        |language     |
|name                |name        |symbol      |name        |symbol       |
|paren in expression |(           |language    |(           |language     |
|brace in expression |{           |language    |call        |language     |
|S3 lm object        |lm          |list        |list        |list         |
|S4 dummy object     |dummy       |S4          |S4          |S4           |
|external pointer    |externalptr |externalptr |externalptr |externalptr  |

R中现有物体的类型在R 语定义手册中讨论。 这里没有提及几类:只有C级才能测试“promise”、“......”和“ANY”类和“bytecode”和“weakref”。

The table of available types in the R source is here.

www.un.org/Depts/DGACM/index_spanish.htm R中是否有(实际为一)类?

事实上,这肯定不是权利:

> x <- 3
> class(x) <- c("hi","low")
> class(x)
[1] "hi"  "low"

一切都有(至少一个)。

www.un.org/Depts/DGACM/index_spanish.htm R是否有(实际上的)模式?

不是肯定的,但我怀疑。

www.un.org/Depts/DGACM/index_spanish.htm 哪类东西告诉我们?

<>代码> 表示物体的内部类型。 根据<代码>可能达到的价值? 的类型:

The vector types "logical", "integer", "double", "complex", "character", "raw" and "list", "NULL", "closure" (function), "special" and "builtin" (basic functions and operators), "environment", "S4" (some S4 objects) and others that are unlikely to be seen at user level ("symbol", "pairlist", "promise", "language", "char", "...", "any", "expression", "externalptr", "bytecode" and "weakref").

<代码>mode依靠类型。 http://www.un.org。 模式/代码:

Modes have the same set of names as types (see typeof) except that types "integer" and "double" are returned as "numeric". types "special" and "builtin" are returned as "function". type "symbol" is called mode "name". type "language" is returned as "(" or "call".

www.un.org/Depts/DGACM/index_spanish.htm 需要什么其他信息来充分描述一个实体? (例如,名单存放在哪里?)

名单包括:

> y <- list(3)
> class(y)
[1] "list"

你们是否意味着病媒化? 其目的应足以:

> z <- 3
> class(z)
[1] "numeric"
> length(z)
[1] 1

参考3,作为长度1的计数媒介,而不是一些原始数字类型。

<><>Conclusion>/strong>

可以通过<条码>和<条码>获得简单罚款。 在你需要其他 st的时候,你可能不得不问一下:

添加以下几个问题:

  • What other information is needed to fully describe an entity?

除<条码>、<条码>、<>条码>、<>条码>、<条码>、<条码>、<条码>、<条码>、<条码>、>等外,还应注意到<条码>各项指示/代码>。

is(1)
[1] "numeric" "vector"

虽然有用,但它也不能令人满意。 举例来说,<代码>1不止是这样;它也是原子、定型和双重的。 下述功能应显示所有物体均符合<代码>is.(......)功能:

what.is <- function(x, show.all=FALSE) {

  # set the warn option to -1 to temporarily ignore warnings
  op <- options("warn")
  options(warn = -1)
  on.exit(options(op))

  list.fun <- grep(methods(is), pattern = "<-", invert = TRUE, value = TRUE)
  result <- data.frame(test=character(), value=character(), 
                       warning=character(), stringsAsFactors = FALSE)

  # loop over all "is.(...)" functions and store the results
  for(fun in list.fun) {
    res <- try(eval(call(fun,x)),silent=TRUE)
    if(class(res)=="try-error") {
      next() # ignore tests that yield an error
    } else if (length(res)>1) {
      warn <- "*Applies only to the first element of the provided object"
      value <- paste(res,"*",sep="")
    } else {
      warn <- ""
      value <- res
    }
    result[nrow(result)+1,] <- list(fun, value, warn)
  }

  # sort the results
  result <- result[order(result$value,decreasing = TRUE),]
  rownames(result) <- NULL

  if(show.all)
    return(result)
  else
    return(result[which(result$value=="TRUE"),])
}

因此,我们现在有了更完整的情况:

> what.is(1)
        test value warning
1  is.atomic  TRUE        
2  is.double  TRUE        
3  is.finite  TRUE        
4 is.numeric  TRUE        
5  is.vector  TRUE 

> what.is(CO2)
           test value warning
1 is.data.frame  TRUE        
2       is.list  TRUE        
3     is.object  TRUE        
4  is.recursive  TRUE 

您还获得更多关于以下论点的信息:show.all=TRUE。 我不在此举任何例子,因为结果有50条线以上。

最后,这意味着作为信息的补充来源,而不是取代先前提到的任何其他职能。

http://www.ohchr.org。

为了按照“Erdogan”的意见列入更多的“is”职能,你可以将此范围添加到以下职能中:

  # right after 
  # list.fun <- grep(methods(is), pattern = "<-", invert = TRUE, value = TRUE)
  list.fun.2 <- character()

  packs <- c( base ,  utils ,  methods ) # include more packages if needed

  for (pkg in packs) {
    library(pkg, character.only = TRUE)
    objects <- grep("^is.+\w$", ls(envir = as.environment(paste( package , pkg, sep =  : ))),
                    value = TRUE)
    objects <- grep("<-", objects, invert = TRUE, value = TRUE)
    if (length(objects) > 0) 
      list.fun.2 <- append(list.fun.2, objects[sapply(objects, function(x) class(eval(parse(text = x))) == "function")])
  }

  list.fun <- union(list.fun.1, list.fun.2)  

  # ...and continue with the rest
  result <- data.frame(test=character(), value=character(), 
                       warning=character(), stringsAsFactors = FALSE)
  # and so on...

这个问题涉及R中各类“类型”的混淆。 在阅读了这个网站和其他一些网站之后,我认为“RichieCotton”的评论最好能解决这一混乱。 在听取这一评论时,我非常感动,我愿强调这一点,把它作为答案。

<[...] at this point,mode and storage.mode<>code> are residue over from S. 只需要注意<条码>()和<条码>(>>>。

因此,在阅读其他答复时,铭记这一点,并侧重于<>条码/代码>和<条码>。 遗憾的是,在其他员额中,<代码>mode和storage.mode并不明显。 不应使用。 我本来会节省很多时间,首先阅读这一评论。





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...