R错误:参数为';其中';这是不合逻辑的
我有一个带有文件名的向量。向量中的几个文件以不同版本重复。比如说 “ConsoleKit2-1.0.0-x86_64-3” “ConsoleKit2-1.0.0-x86_64-4” “Cython-0.23.4-x86_64-1” “Cython-0.29.12-x86_64-1” “GConf-3.2.6-x86_64-3” “GConf-3.2.6-x86_64-4” “LibRaw-0.17.2-x86_64-1” “LibRaw-0.18.12-x86_64-1” “M2Crypto-0.23.0-x86_64-1” “M2Crypto-0.35.2-x86_64-1” “MPlayer-1.2_20160125-x86_64-3” “MPlayer-20190418-x86_64-1” “Mako-1.0.13-x86_64-1” “ModemManager-1.10.4-x86_64-1” “ModemManager-1.4.14-x86_64-1” “NetworkManager-1.18.1-x86_64-1” “NetworkManager-1.2.2-x86_64-2” “PyQt-4.11.4-x86_64-1” “PyQt-4.12.1-x86_64-3” “QSCINTILA-2.10.8-x86_64-2” 我想创建一个新的向量,该向量只包含每个文件的最新版本,这些文件包含多个版本。例如,新列表将以 “ConsoleKit2-1.0.0-x86_64-4” “Cython-0.29.12-x86_64-1” 我列出了一个有多个版本的文件列表,以便和所有文件的列表进行比较R错误:参数为';其中';这是不合逻辑的,r,R,我有一个带有文件名的向量。向量中的几个文件以不同版本重复。比如说 “ConsoleKit2-1.0.0-x86_64-3” “ConsoleKit2-1.0.0-x86_64-4” “Cython-0.23.4-x86_64-1” “Cython-0.29.12-x86_64-1” “GConf-3.2.6-x86_64-3” “GConf-3.2.6-x86_64-4” “LibRaw-0.17.2-x86_64-1” “LibRaw-0.18.12-x86_64-1” “M2Crypto-0
dput(fix1[1:30])
c("ConsoleKit2", "Cython", "GConf", "LibRaw", "M2Crypto", "MPlayer",
"ModemManager", "NetworkManager", "PyQt", "QScintilla", "Thunar",
"a2ps", "a52dec", "aaa_base", "aaa_elflibs", "aaa_terminfo",
"aalib", "acct", "acl", "acpid", "adwaita-icon-theme", "akonadi",
"alpine", "alsa-lib", "alsa-oss", "alsa-plugins", "alsa-utils",
"amarok", "amor", "amp")
我编写了一个小函数来比较完整列表和具有多个版本的文件列表,但是我一直得到一个我不理解的错误
这个函数有几个步骤,我在这里简化了这个函数,因为错误发生得很早
> ser2 <- function(in1, ... ) {
+ out1 = NULL #output
+ l1 <- hd1 #list of all files
+ for (i in 1:length(in1 )) {
+ out1[i] = l1[which(grepl(in1[i], l1))][1] #start to filter the main list by list of dupes
+ }
+ return(out1)
+ }
>
>
> ser2(fix1)
Error in which(grepl(in1[i], l1)) : argument to 'which' is not logical
> traceback()
2: which(grepl(in1[i], l1)) at #5
1: ser2(fix1)
编辑-在nograps的回答之后,我稍微更改了sample函数。关于
which()
的参数不符合逻辑,我仍然会遇到相同的错误 对我来说,你的代码不会以同样的方式失败。如果我跑步:
hd1 <- c("ConsoleKit2-1.0.0-x86_64-3", "ConsoleKit2-1.0.0-x86_64-4",
"Cython-0.23.4-x86_64-1", "Cython-0.29.12-x86_64-1", "GConf-3.2.6-x86_64-3",
"GConf-3.2.6-x86_64-4", "LibRaw-0.17.2-x86_64-1", "LibRaw-0.18.12-x86_64-1",
"M2Crypto-0.23.0-x86_64-1", "M2Crypto-0.35.2-x86_64-1", "MPlayer-1.2_20160125-x86_64-3",
"MPlayer-20190418-x86_64-1", "Mako-1.0.13-x86_64-1", "ModemManager-1.10.4-x86_64-1",
"ModemManager-1.4.14-x86_64-1", "NetworkManager-1.18.1-x86_64-1",
"NetworkManager-1.2.2-x86_64-2", "PyQt-4.11.4-x86_64-1", "PyQt-4.12.1-x86_64-3",
"QScintilla-2.10.8-x86_64-2")
fix1 <- c("ConsoleKit2", "Cython", "GConf", "LibRaw", "M2Crypto", "MPlayer",
"ModemManager", "NetworkManager", "PyQt", "QScintilla", "Thunar",
"a2ps", "a52dec", "aaa_base", "aaa_elflibs", "aaa_terminfo",
"aalib", "acct", "acl", "acpid", "adwaita-icon-theme", "akonadi",
"alpine", "alsa-lib", "alsa-oss", "alsa-plugins", "alsa-utils",
"amarok", "amor", "amp")
ser2 <- function(in1, ... ) {
out1 = NULL #output
l1 <- hd1 #list of all files
for (i in 1:length(in1 )) {
dec1 = l1[which(grepl(in1[i], l1))][1] #start to filter the main list by list of dupes
dec2 = l1[which(grepl(in1[i], l1))][2]
}
return(list(dec1, dec2))
}
ser2(fix1)
它所做的是对fix1
的所有i
值运行grep(fix1[i],hd1,value=TRUE)
,并将它们粘贴到列表中
假设hd1
按从早到晚的顺序排序,您可以使用tail
获得每个值的最新值
matches <- lapply(fix1, grep, hd1, value = TRUE)
unlist(lapply(matches, tail, 1))
# [1] "ConsoleKit2-1.0.0-x86_64-4" "Cython-0.29.12-x86_64-1"
# [3] "GConf-3.2.6-x86_64-4" "LibRaw-0.18.12-x86_64-1"
# [5] "M2Crypto-0.35.2-x86_64-1" "MPlayer-20190418-x86_64-1"
# [7] "ModemManager-1.4.14-x86_64-1" "NetworkManager-1.2.2-x86_64-2"
# [9] "PyQt-4.12.1-x86_64-3" "QScintilla-2.10.8-x86_64-2"
匹配项是否在函数中声明为hd1
?我会尝试打印I
值,以便检查代码中断的点。然后我会在for循环的开头添加一个if,使用browser()
。如果代码在第一次迭代中收支平衡,我只需在for循环中添加浏览器来检查变量的值……我在for循环的最开始添加了一条print()
语句。原来我的fix1
列表中有一些我不知道的NA值。谢谢你的建议。我还是会犯同样的错误。《核不扩散条约》的评论帮助我找出了问题所在:那里有一些我没有意识到的NAs。但是,您对最后两行代码的建议有很大帮助<代码>hd1
不是从最早到最晚排序的。因此,我遗漏的代码(一些if/else和“>”以及更多的行)应该是将版本/日期信息分离出来并从中选择。
hd1 <- c("ConsoleKit2-1.0.0-x86_64-3", "ConsoleKit2-1.0.0-x86_64-4",
"Cython-0.23.4-x86_64-1", "Cython-0.29.12-x86_64-1", "GConf-3.2.6-x86_64-3",
"GConf-3.2.6-x86_64-4", "LibRaw-0.17.2-x86_64-1", "LibRaw-0.18.12-x86_64-1",
"M2Crypto-0.23.0-x86_64-1", "M2Crypto-0.35.2-x86_64-1", "MPlayer-1.2_20160125-x86_64-3",
"MPlayer-20190418-x86_64-1", "Mako-1.0.13-x86_64-1", "ModemManager-1.10.4-x86_64-1",
"ModemManager-1.4.14-x86_64-1", "NetworkManager-1.18.1-x86_64-1",
"NetworkManager-1.2.2-x86_64-2", "PyQt-4.11.4-x86_64-1", "PyQt-4.12.1-x86_64-3",
"QScintilla-2.10.8-x86_64-2")
fix1 <- c("ConsoleKit2", "Cython", "GConf", "LibRaw", "M2Crypto", "MPlayer",
"ModemManager", "NetworkManager", "PyQt", "QScintilla", "Thunar",
"a2ps", "a52dec", "aaa_base", "aaa_elflibs", "aaa_terminfo",
"aalib", "acct", "acl", "acpid", "adwaita-icon-theme", "akonadi",
"alpine", "alsa-lib", "alsa-oss", "alsa-plugins", "alsa-utils",
"amarok", "amor", "amp")
ser2 <- function(in1, ... ) {
out1 = NULL #output
l1 <- hd1 #list of all files
for (i in 1:length(in1 )) {
dec1 = l1[which(grepl(in1[i], l1))][1] #start to filter the main list by list of dupes
dec2 = l1[which(grepl(in1[i], l1))][2]
}
return(list(dec1, dec2))
}
ser2(fix1)
lapply(fix1, grep, hd1, value = TRUE)
matches <- lapply(fix1, grep, hd1, value = TRUE)
unlist(lapply(matches, tail, 1))
# [1] "ConsoleKit2-1.0.0-x86_64-4" "Cython-0.29.12-x86_64-1"
# [3] "GConf-3.2.6-x86_64-4" "LibRaw-0.18.12-x86_64-1"
# [5] "M2Crypto-0.35.2-x86_64-1" "MPlayer-20190418-x86_64-1"
# [7] "ModemManager-1.4.14-x86_64-1" "NetworkManager-1.2.2-x86_64-2"
# [9] "PyQt-4.12.1-x86_64-3" "QScintilla-2.10.8-x86_64-2"