簡體   English   中英

R編程:這個for循環的更靈活的版本

[英]R programming: more flexible version of this for loop

下面是我的R代碼,它采用向量a並返回向量b。 向量b應該是具有特定格式的向量a的唯一標識符。 請注意,a使用彼此相鄰的所有相同數字進行排序。

a <- c(1, 1, 1, 2, 2, 2, 3, 4, 5, 6, 6, 6, 6, 7, 8, 9, 9)
b <- NULL


for(i in 5:length(a)){
        if (a[i] == a[i - 1] & a[i] == a[i - 2] & a[i] == a[i - 3] & a[i] == a[i - 4])
            b[i] <- paste(a[i], "-", 4, sep="")
        else if (a[i] == a[i - 1] & a[i] == a[i - 2] & a[i] == a[i - 3])
            b[i] <- paste(a[i], "-", 3, sep="")
        else if (a[i] == a[i - 1] & a[i] == a[i - 2])
            b[i] <- paste(a[i], "-", 2, sep="")
        else if (a[i] == a[i - 1])
            b[i] <- paste(a[i], "-", 1, sep="")
        else 
            b[i] <- paste(a[i], "-", 0, sep="")
}

#The first 4 values in vector b have to manually entered 
#because the for loop checks up to 4 consecutive numbers in a
b[1] <- "1-0" 
b[2] <- "1-1"
b[3] <- "1-2"
b[4] <- "2-0"

b

上面的代碼根據需要返回b,但是,如果向量a具有多於4個相同的連續數,則for循環將產生包含一些相同元素的b。 如何改進這種for循環,以便可以為任何數量的相同連續數字賦予適當的唯一標識符。

我正在考慮使用某種嵌套的for循環,但是如何在if語句中完成呢?

這可能會取代您當前的循環。 rle()被用於構建序列的每個唯一元件a ,從零開始。 然后我們可以將它們與-分隔符paste()在一起。

paste(a, sequence(rle(a)$lengths) - 1, sep = "-")
#  [1] "1-0" "1-1" "1-2" "2-0" "2-1" "2-2" "3-0" "4-0" "5-0" "6-0" "6-1"
# [12] "6-2" "6-3" "7-0" "8-0" "9-0" "9-1"

這與b的輸出相同

使用avepaste ,我現在意識到這基本上只是@ RichardScriven答案的變體:

paste(a, ave(a,a,FUN=seq_along) - 1, sep="-")
# [1] "1-0" "1-1" "1-2" "2-0" "2-1" "2-2" "3-0" "4-0" "5-0" "6-0" "6-1"
#[12] "6-2" "6-3" "7-0" "8-0" "9-0" "9-1"
# If you are sure the different groups are really sorted, this will work:
b <- tapply(1:length(a), a, FUN = function(x) (1:length(x)) -1 )
b <- paste(a, unlist(b), sep = "-")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM