a
各要素が見つかったベクトルの名前とそのベクトル内の位置を返す関数が必要だと思います。
これはきれいではありませんが、あなたが望むことをするべきです:
fun1 <- function(i) {
res <- NULL
if (a[i] %in% x) res <- paste("x",which(x==a[i]),sep="")
if (a[i] %in% y) res <- c(res, paste(" y",which(y==a[i]),sep=""))
names(res) <- rep(a[i],length(res))
return(res)
}
それから
unlist(sapply(1:length(a), FUN = fun1))
与える
0 0 1 2 2 4 6 6 8 Inf
"x1" "x2" " y1" "x3" " y2" "x4" " y3" " y4" "x5" " y5"
お探しのアップデートはこれですか?
fun1 <- function(i) {
res1 <- res2 <- NULL
if (a[i] %in% x) res1 <- paste("x", which(x %in% a[i]),sep="",collapse=" ")
if (a[i] %in% y) res2 <- paste("y", which(y %in% a[i]),sep="",collapse=" ")
res <- paste(res1,res2,collapse="")
names(res) <- a[i]
ascii:::trim(res)
}
これは、以前と同じ使用法で、
0 1 2 4 6 8 Inf
"x1 x2" "y1" "x3 y2" "x4" "y3 y4" "x5" "y5"