gpt4 book ai didi

r - 为什么 rownames(installed.packages()) 有 names 属性?

转载 作者:行者123 更新时间:2023-12-04 11:51:16 27 4
gpt4 key购买 nike

我有以下功能供个人使用。它需要一个作者的名字来查看我的机器上是否有他们的任何软件包。

authoredPackages <- function (author) 
{
s <- sapply(rownames(installed.packages()),
packageDescription, fields = "Author")
names(grep(author, s, value = TRUE))
}

这就是问题所在。在打开一个新的 R session 并分配函数后,第一次调用函数 总是 返回一个空字符串的字符向量,它是它应该返回的向量的正确长度。要显示这一点,请打开一个新的 R session ,分配函数,并使用您最喜欢的包作者的姓氏运行它。它应该首先返回一个空的字符向量......
authoredPackages("Temple Lang")
# [1] "" "" "" ""

...然后再做一次,它返回正确的结果......
authoredPackages("Temple Lang")
# [1] "jsonlite" "RCurl" "RJSONIO" "XML"

它总是只在新 R session 中的第一次调用时发生。为什么会发生这种情况,我该如何修复它才能使该功能在第一次尝试时始终有效?

我的 R --vanilla session 信息:
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

更新:打开 R 后,似乎是 rownames(installed.packages())有一个 names属性是因为 lme4包裹。不知道为什么,这是唯一的名字。它在第二次调用时如何消失也很奇怪。
rownames(installed.packages())[228]
# ret0
# "lme4"

最佳答案

奇怪,但似乎是 rownames(installed.packages())有一个 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 并再次尝试。我仍然得到相同的奇怪结果,如果我使用 @MartinMorgan,我会始终如一地得到它们。的建议使用 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

关于r - 为什么 rownames(installed.packages()) 有 names 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27116593/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com