作者:手机用户2502876217 | 来源:互联网 | 2023-05-26 20:39
下面是我的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语句中完成呢?
1> Rich Scriven..:
这可能会取代您当前的循环. 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