我有一个标准的"我可以避免循环"问题,但无法找到解决方案.
我在@splaisan回答了这个问题,但我不得不在中间部分采用一些丑陋的扭曲,for并进行多次if测试.我在这里模拟一个更简单的版本,希望有人能给出更好的答案......
问题
给定这样的数据结构:
df <- read.table(text = 'type
a
a
a
b
b
c
c
c
c
d
e', header = TRUE)
Run Code Online (Sandbox Code Playgroud)
我想识别相同类型的连续块并将它们分组.第一个块应标记为0,下一个块应标记为0,依此类推.存在无限数量的块,并且每个块可以仅与一个成员一样短.
type label
a 0
a 0
a 0
b 1
b 1
c 2
c 2
c 2
c 2
d 3
e 4
Run Code Online (Sandbox Code Playgroud)
我的解决方案
我不得不求助于for循环来执行此操作,这里是代码:
label <- 0
df$label <- label
# LOOP through the label column and increment the label
# whenever a new …Run Code Online (Sandbox Code Playgroud) 我在这里绘制染色体的长度值

没有点的中间区域不包含数据,不应该得到黄土线.如何修改我的代码以阻止该区域的黄土线?数据是连续的但我可以添加行来标记具有一些特殊值的空白区域或添加带有标签的列?但如何在命令中使用它?
我当前的命令:
library(IDPmisc)
# plot settings (edit here)
spanv<-0.05
pointcol1="#E69F00"
pointcol2="#56B4E9"
pointcol3="#009E73"
points=20
linecol="green"
xlabs=paste(onechr, " position", " (loess-span=", spanv, ")", sep="")
data1<-NaRV.omit(data[,c(2,7)]) # keep only x and y for the relevant data
# and clean NA and Inf
ylabs='E / A - ratio'
p1<-ggplot(data1, aes(x=start, y=E.R)) +
ylim(0,5) +
geom_point(shape=points, col=pointcol1, na.rm=T) +
geom_hline(aes(yintercept=1, col=linecol)) +
geom_smooth(method="loess", span=spanv, fullrange=F, se=T, na.rm=T) +
xlab(xlabs) +
ylab(ylabs)
Run Code Online (Sandbox Code Playgroud)