我有一个字符串,我想从序列中提取咒语--例如,
A<- c('000001111000', '0110011', '110001')
我想要连续的拼写长度为0和1的序列格式。然后用咒语的长度来计算描述性统计数据,如均值、模式、sd等(spell_0和spell_1是A向量的序列)。
例如,
spell_0 spell_1 mean_spell_0 mean_spell_1
5-3 4 4 4
1-2 2-2 1.5 2
3 2-1 3 1.5
有什么建议吗?
发布于 2021-10-25 14:17:05
你的问题实际上包括几个问题。
从原始向量中,首先需要获得不同的序列,然后将字符串拆分为字符。正如注释中指出的那样,这可以通过rle
实现。然后,对于示例中的每个值("0“和"1"),您需要获得与该值对应的每个序列的lengths
。然后,您需要将它们放在您想要的格式中(尽管这可能不是最合适的)。
我的建议是这样做:
seqA <- lapply(strsplit(A, ""), rle)
do.call(cbind,lapply(c("0", "1"), # this can be made more general, for example using unique(unlist(strsplit(A, "")))
function(i){
do.call(rbind, lapply(seqA,
function(x){
lesSeq <- x$lengths[x$values==i]
res <- data.frame(paste(lesSeq, collapse="-"), mean(lesSeq))
colnames(res) <- paste(c("spell", "mean_spell"), i, sep="_")
return(res)
}))
}))[, c(1, 3, 2, 4)] # this rearrangment may not be needed...
# spell_0 spell_1 mean_spell_0 mean_spell_1
#1 5-3 4 4.0 4.0
#2 1-2 2-2 1.5 2.0
#3 3 2-1 3.0 1.5
发布于 2021-10-25 14:16:37
你可以试试这样的方法:
do.call(rbind,
lapply(strsplit(A, ""),
function(x) {
lengths <- rle(x)$lengths
values <- rle(x)$values
data.frame(spell_0 = paste(lengths[values == "0"], collapse = "-"),
spell_1 = paste(lengths[values == "1"], collapse = "-"),
mean_spell_0 = mean(lengths[values == "0"]),
mean_spell_1 = mean(lengths[values == "1"]))
}))
#> spell_0 spell_1 mean_spell_0 mean_spell_1
#> 1 5-3 4 4.0 4.0
#> 2 1-2 2-2 1.5 2.0
#> 3 3 2-1 3.0 1.5
发布于 2021-10-25 14:28:21
首先提取并计数0
s和1
s:
library(stringr)
spell_0a <- sapply(str_extract_all(A, "0+"), function(x) str_count(x, "0"))
spell_1a <- sapply(str_extract_all(A, "1+"), function(x) str_count(x, "1"))
然后我们对结果进行分解,并进行数学运算:
df <- data.frame(
# collapse results:
spell_0 = unlist(lapply(spell_0a, function(x) paste0(x, collapse = "-"))),
spell_1 = unlist(lapply(spell_1a, function(x) paste0(x, collapse = "-"))),
# calculate means:
mean_spell_0 = unlist(lapply(spell_0a, function(x) ifelse(length(x)==1, x[1], sum(x[1]+x[2])/2))),
mean_spell_1 = unlist(lapply(spell_1a, function(x) ifelse(length(x)==1, x[1],sum(x[1]+x[2])/2)))
)
结果:
df
spell_0 spell_1 mean_spell_0 mean_spell_1
1 5-3 4 4.0 4.0
2 1-2 2-2 1.5 2.0
3 3 2-1 3.0 1.5
https://stackoverflow.com/questions/69709370
复制相似问题