奇怪,但看起来像rownames(installed.packages())
has a names
第一次调用它时的属性。
> str(rownames(installed.packages()))
Named chr [1:125] "bdsmatrix" "bitops" "blotter" "brew" "car" "changepoint" "chron" "colorout" ...
- attr(*, "names")= chr [1:125] "" "" "" "" ...
> str(rownames(installed.packages()))
chr [1:125] "bdsmatrix" "bitops" "blotter" "brew" "car" "changepoint" "chron" "colorout" "colorspace" ...
抱歉,这个问题留给你来回答了。只要确保没有名字即可。这对你来说是个问题,因为你依赖sapply
的默认值是USE.NAMES=TRUE
,但这只会添加尚不存在的名称。他们的出现是出于一些非常奇怪的原因。
authoredPackages <- function (author)
{
r <- setNames(rownames(installed.packages()), NULL)
s <- sapply(r, function(x) packageDescription(x)$Author)
names(grep(author, s, value = TRUE))
}
这是我的sessionInfo
(从开始R --vanilla
):
> sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.1.1
我刚刚升级到 R-3.1.2 并再次尝试。我仍然得到相同的奇怪结果,并且如果我使用,我会一致地得到它们@马丁摩根 https://stackoverflow.com/questions/27116593/why-does-rownamesinstalled-packages-have-a-names-attribute/27116647#comment42737154_27116593的使用建议noCache=TRUE
.
> str(rownames(installed.packages(noCache=TRUE)))
Named chr [1:125] "bdsmatrix" "bitops" "blotter" "brew" "car" "changepoint" "chron" "colorout" ...
- attr(*, "names")= chr [1:125] "" "" "" "" ...
> str(rownames(installed.packages(noCache=TRUE)))
Named chr [1:125] "bdsmatrix" "bitops" "blotter" "brew" "car" "changepoint" "chron" "colorout" ...
- attr(*, "names")= chr [1:125] "" "" "" "" ...
> str(rownames(installed.packages(noCache=TRUE)))
Named chr [1:125] "bdsmatrix" "bitops" "blotter" "brew" "car" "changepoint" "chron" "colorout" ...
- attr(*, "names")= chr [1:125] "" "" "" "" ...
> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] setwidth_1.0-3 colorout_1.0-1
loaded via a namespace (and not attached):
[1] tools_3.1.2