Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 根据A列第N行和B列第N行的值之差插入行+;1._R - Fatal编程技术网

R 根据A列第N行和B列第N行的值之差插入行+;1.

R 根据A列第N行和B列第N行的值之差插入行+;1.,r,R,我有如下示例的数据(我使用R): 我想在A列第N+1行和B列第N>1行与C行之间的差异得到标签的地方添加行(例如“其他”)。因此,数据如下所示: A B C 1 2 Background 3 19 Background 20 25 other 26 41 person 43 69 person 70 82 other 83 97 Background 98 106 other 107 129 Background 130 131 other 132 1

我有如下示例的数据(我使用
R
):

我想在A列第N+1行和B列第N>1行与C行之间的差异得到标签的地方添加行(例如“其他”)。因此,数据如下所示:

A   B   C
1   2   Background
3   19  Background
20  25  other
26  41  person
43  69  person
70  82  other
83  97  Background
98  106 other
107 129 Background
130 131 other
132 179 Background
180 188 other
189 235 Background
236 242 other
243 258 Background
259 260 other
261 279 person

谢谢

这里有一种使用基数R的方法,假设第四行
A
的值是42(而不是43)

#找出N+1行和
#第N行中的B值不等于1。

inds这里有一种使用基数R的方法,假设第四行
A
的值是42(而不是43)

#找出N+1行和
#第N行中的B值不等于1。

inds使用
数据的选项。表
使用与Ronak相同的数据编辑:

ix <- DT[shift(A, -1L) - B > 1L, which=TRUE]
rbindlist(list(DT,
    data.table(A=DT$B[ix]+1L, B=DT$A[ix+1L]-1L, C="other")))[order(A)]
数据:

库(data.table)

DT使用
数据的选项。表
使用与Ronak相同的数据编辑:

ix <- DT[shift(A, -1L) - B > 1L, which=TRUE]
rbindlist(list(DT,
    data.table(A=DT$B[ix]+1L, B=DT$A[ix+1L]-1L, C="other")))[order(A)]
数据:

库(data.table)

DT理想情况下,解决方案应该包括一种跳过的方法,但我意识到这可能会有问题-最终它可以很好地工作,当我的差值只有1时,我可以事后过滤掉该行。如果混合中有第四列,我该怎么办?我遇到一个错误…假设第1-5行的id==1,第6-10行的id==2?在这种情况下,您希望给出
id
的值是多少?或者您想分别对每个
id
执行相同的操作?我希望保持id等于其原始值(因此,如果第1-5行的id==1,则include_df包含该信息)。我正致力于对每个id应用相同的操作,这并不难,但我认为在您在base R中提供的答案中可能有一个更简单的解决方案。我认为如果在
include_df
数据帧调用中包含
id=df$id[inds]
,它应该可以正常工作
include_df理想情况下,该解决方案会包含一种跳过的方法,但我意识到这可能会有问题-最终,如果我的差异只有1,我可以在事后过滤掉该行。如果混合中有第4列,我该怎么办?我遇到一个错误…假设第1-5行的id==1,第6-10行的id==2?在这种情况下,您希望给出
id
的值是多少?或者您想分别对每个
id
执行相同的操作?我希望保持id等于其原始值(因此,如果第1-5行的id==1,则include_df包含该信息)。我正致力于对每个id应用相同的操作,这并不难,但我认为在您在base R中提供的答案中可能有一个更简单的解决方案。我认为如果在
include_df
数据帧调用中包含
id=df$id[inds]
,它应该可以正常工作<代码>包括
df <- structure(list(A = c(1, 3, 26, 42, 83, 107, 132, 189, 243, 261
), B = c(2L, 19L, 41L, 69L, 97L, 129L, 179L, 235L, 258L, 279L
), C = c("Background", "Background", "person", "person", "Background", 
"Background", "Background", "Background", "Background", "person"
)), row.names = c(NA, -10L), class = "data.frame")
ix <- DT[shift(A, -1L) - B > 1L, which=TRUE]
rbindlist(list(DT,
    data.table(A=DT$B[ix]+1L, B=DT$A[ix+1L]-1L, C="other")))[order(A)]
      A   B          C
 1:   1   2 Background
 2:   3  19 Background
 3:  20  25      other
 4:  26  41     person
 5:  42  69     person
 6:  70  82      other
 7:  83  97 Background
 8:  98 106      other
 9: 107 129 Background
10: 130 131      other
11: 132 179 Background
12: 180 188      other
13: 189 235 Background
14: 236 242      other
15: 243 258 Background
16: 259 260      other
17: 261 279     person
library(data.table)
DT <- fread("A   B   C
1   2   Background
3   19  Background
26  41  person
42  69  person
83  97  Background
107 129 Background
132 179 Background
189 235 Background
243 258 Background
261 279 person")