使用 RI 时始终牢记:“尽可能避免使用循环”。但是,我现在被困住了,我无法找到一种疯狂的方式来编写我需要的代码。
郑重声明,经过几条评论,我上面的说法不是正确的说法,这里没有必要避免循环以提高效率。
我有两个字符串向量作为输入,让我们称它们为-它们只能包含字母,a
和。b
"M"
"I"
"D"
a = c("M","I","D","D","M","M","M","M","M","M")
b = c("M","M","M","M","M","M","D","M","M")
我想要的输出是:
d = c("M","I","D","D","M","M","M","M","I","M","M")
以下函数给了我这样的输出:
my.function <- function(a, b)
{
nrow.df = length(a) + length(which(b=="D"))
my.df = data.frame(a = rep(NA, nrow.df),
b = rep(NA, nrow.df),
d = rep(NA, nrow.df))
my.df$a[1:length(a)] = a
my.df$b[1:length(b)] = b
for (i in 1:nrow.df)
{
if(my.df$a[i] == "D") {
my.df$d[i] = "D"
my.df$b[(i+1):nrow.df] = my.df$b[i:(nrow.df-1)]
} else if (my.df$b[i] == "D") {
my.df$d[i] = "I"
my.df$a[(i+1):nrow.df] = my.df$a[i:(nrow.df-1)]
} else if (my.df$a[i] == "I") {
my.df$d[i] = "I"
} else if (my.df$b[i] == "I") {
my.df$d[i] = "D"
} else {
my.df$d[i] = my.df$a[i]
}
}
return(my.df$d)
}
> d = my.function(a,b)
> d
[1] "M" "I" "D" "D" "M" "M" "M" "M" "I" "M" "M"
函数逻辑如下,只要有 a "D"
in a
,就将 a "D"
in放入并将d
向量移位b
1,反之亦然,只要有 a "D"
in b
,就将 a in 放入"I"
并d
移位a
1。
接下来,当有一个"I"
in a
,但没有一个"D"
in 时b
,放一个"I"
in a
,反之亦然,只要有一个"I"
in b
,而不是一个"D"
in a
,就放一个"D"
in d
。否则,d = a
。
它不是一个复杂的功能,但我正在努力使其 R 高效。我用 mclapply 应用了这个函数数百万次,所以快速实现这个函数可以为我节省很多时间。
你推荐使用 Rcpp 吗?会不会快很多?数百万次与 Cpp 通信 R 是否有任何减慢,或者它只是与 Rcpp 自动通信?