[英]How to remove character in specific rows in R?
如果特定值在V1中,我想將其從下一行的V2列中刪除。
輸入:
V1 V2
19 49426099 19 49469087
19 49469087 19 49426099
6 29910378 6 29910742 - 6 29911064 - 6 29911086 - 6 29911092 - 6 29911154
6 29910742 6 29910378 - 6 29911064 - 6 29911086 - 6 29911092 - 6 29911154
6 29911064 6 29910378 - 6 29910742 - 6 29911086 - 6 29911092 - 6 29911154
我期望這個結果:
V1 V2
19 49426099 19 49469087
19 49469087
6 29910378 6 29910742 - 6 29911064 - 6 29911086 - 6 29911092 - 6 29911154
6 29910742 6 29911064 - 6 29911086 - 6 29911092 - 6 29911154
6 29911064 6 29911086 - 6 29911092 - 6 29911154
這是一個示例數據框:
d = data.frame(V1 = c('19 49426099','19 49469087','6 29910378','6 29910742','6 29911064'), V2 = c('19 49469087','19 49426099','6 29910742 - 6 29911064 - 6 29911086 - 6 29911092 - 6 29911154','6 29910378 - 6 29911064 - 6 29911086 - 6 29911092 - 6 29911154','6 29910378 - 6 29910742 - 6 29911086 - 6 29911092 - 6 29911154'))
d$V1 = as.character(d$V1)
d$V2 = as.character(d$V2)
這是一個解決方案:
r = sapply(0:(nrow(d) - 1), function(i){
gsub(pattern = paste(d[1:i, 'V1'], collapse = '|'),'', x = d[i+1, 'V2'])
})
r = gsub(pattern = ' - - ', replacement = '', x = r) # remove the gaps where numbers were removed
r = gsub(pattern = '^ - ', replacement = '', x = r) # remove gaps at the beginning
r = gsub(pattern = ' - $', replacement = '', x = r) # in case you have gaps at the end
d$V2 = r
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.