![](/img/trans.png)
[英]Filling in empty elements on a vector from another vector by position [R]
[英]Position of elements from one vector in another vector with R
我希望創建一個vector
,其中包含來自另一個vector
中的一個vector
的elements
的 position 。 這類似於以下問題:
基礎R
中的match
function 在最簡單的情況下工作,如下所示:
a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,2,3,4,5)
desired.output <- c(1,3,5,7,9)
match(b,a)
#[1] 1 3 5 7 9
但是,在下面顯示的更復雜的情況下, match
似乎不起作用。 我可能需要which
和match
的組合。 到目前為止,在每種情況下,我正在考慮的 b 中的值在b
中的出現頻率並不比在b
中a
頻率高。 我需要一個基本的R
解決方案。
a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,2,2,3,4,5)
desired.output <- c(1,3,4,5,7,9)
a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,2,2,3,4,4,5)
desired.output <- c(1,3,4,5,7,8,9)
a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,2,2,3,4,4,5,5)
desired.output <- c(1,3,4,5,7,8,9,10)
a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,1,2,2,3,4,4,5,5)
desired.output <- c(1,2,3,4,5,7,8,9,10)
a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,1,2,2,3,3,4,4,5,5)
desired.output <- c(1,2,3,4,5,6,7,8,9,10)
您正在尋找pmatch
:
a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,2,3,4,5)
pmatch(b,a)
#[1] 1 3 5 7 9
a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,2,2,3,4,5)
pmatch(b,a)
#[1] 1 3 4 5 7 9
a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,2,2,3,4,4,5)
pmatch(b,a)
#[1] 1 3 4 5 7 8 9
a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,2,2,3,4,4,5,5)
pmatch(b,a)
#[1] 1 3 4 5 7 8 9 10
a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,1,2,2,3,4,4,5,5)
pmatch(b,a)
#[1] 1 2 3 4 5 7 8 9 10
a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,1,2,2,3,3,4,4,5,5)
pmatch(b,a)
# [1] 1 2 3 4 5 6 7 8 9 10
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.