- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我真的不明白 FUN.VALUE
参数在 vapply
.
这是我的例子:
a = list(list(1,2), list(1), list(1,2,3))
# give the lengths of each list in a
sapply(a, length)
vapply
使其类型安全而不是
sapply
# gives me same result as sapply
vapply(a, length, FUN.VALUE=1)
# same result, but why?
vapply(a, length, FUN.VALUE=1000)
# gives me error
vapply(a, length, FUN.VALUE="integer")
# gives me error
vapply(a, length, FUN.VALUE="vector")
# gives me error
vapply(a, length, FUN.VALUE=c(1,2))
?vapply
我读到了
FUN.VALUE
可以是标量、向量或矩阵,用于匹配输出的类型。关于原因的任何提示
vapply
这样做?
最佳答案
来自 vapply
文件,
FUN.VALUE
(generalized) vector; a template for the return value from FUN. See ‘Details’.
This function checks that all values of
FUN
are compatible with theFUN.VALUE
, in that they must have the same length and type. [Emphasis added]
length()
返回一个长度为 1 的数字(一个整数,如果我们想具体一点的话)。当您使用
FUN.VALUE = 1
,
1
具有与您期望的输出相同的长度和类型,因此检查通过。当您使用
FUN.VALUE = "integer"
,
"integer"
是长度为 1 的字符向量,因此长度检查通过但类型检查失败。
(Types may be promoted to a higher type within the ordering logical < integer < double < complex, but not demoted.)
vapply
对于可能返回整数或 double 值的函数,您应该确保指定类似
FUN.VALUE = 1.0
的内容。
关于r - vapply 中的 FUN.VALUE 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51657605/
我有一个有序因子向量列表,我想在列表的每个元素中找到最低级别。这是一个使用 sapply 的简单练习,但我是在函数内部执行此操作,因此为了安全起见,我想使用 vapply。问题是,使用 vapply
如何让 vapply 返回一个日期向量? (我认为这是一个不同的问题:Returning a vector of class POSIXct with vapply): f1 <- function(
这个问题有点两面性。首先,我试图找出在向量上实现函数的简单实现的最佳方法。其次,我试图理解 vapply 中 FUN.VALUE 的含义。 我已经定义了一个简单的半波整流函数: Fun = funct
我有如下 POSIXct 类的日期列表(只是一个最小的工作示例): L unlist(L) [1] 1355310732 1355310732 简而言之,如何将 POSIXct 值列表提取到 POS
让我们考虑以下示例: library(ggplot2) zzz <- data.frame(gp = c("a","b","c","d","e","f","g","h","i","j","k","l"
我正在阅读 Hadley 的 Advanced R,网址为 http://adv-r.had.co.nz/Functionals.html .他谈到了 sapply 和 vapply 之间的区别。我的
library(rvest) urls <- c("https://www.r-bloggers.com", "https://www.stackoverflow.com") docsFor <- l
我真的不明白 FUN.VALUE参数在 vapply . 这是我的例子: a = list(list(1,2), list(1), list(1,2,3)) # give the lengths of
我正在尝试关注 good practice并使用 vapply()而不是 sapply()在函数内部,但从 vapply() 找到类型检查想要固定长度时太不灵活了。 假设我想要这样的东西: list1
文档说 vapply is similar to sapply, but has a pre-specified type of return value, so it can be safer [.
在尝试使用 resphape2::dcast reshape 数据时,我遇到了涉及 NA 条目的错误。示例数据在最后。 数据已从长形变为宽形,但有时某些参数的所有 NA 条目似乎是导致问题的原因。或者
我在我的 Ubuntu 10.04 机器上安装了 R 2.10.1 下的 stringr 包。当我尝试使用 str_extract() 函数时,R 停止并给出以下错误消息: 可回收错误(字符串、模式、
hits <- vapply(titles, FUN = grepl, FUN.VALUE = logical(length(pass_names)),
我从来没有非常熟练地使用 R 并且在缺席后回来所以我重新学习了很多。我有一个包含纬度和经度字段的数据集(命名数据)。一些观察值在这些字段中为“0”,这是无效数据。我正在写一个 R 笔记本来记录我的发现
我是一名优秀的程序员,十分优秀!