[英]Assigning values in a sequence depending on previous row value in R
I have asked a similar question like this here and the solution mentioned there works fine with problem stated there but this one is little trickier and harder version of that. 我在这里问了类似这样的问题,并且那里提到的解决方案在那里说的问题工作得很好,但是这个问题比较简单,更难。
I have a data table like this. 我有这样的数据表。
ID1 member
1 a parent
2 a child
3 a parent
4 a child
5 a child
6 b parent
7 b parent
8 b child
9 c child
10 c child
11 c parent
12 c child
And I want to assign a sequence like below keeping in mind ID1 and member column. 我想分配一个如下所示的序列,记住ID1和成员列。
ID1 member sequence
1 a parent 1
2 a child 2
3 a parent 1
4 a child 2
5 a child 3
6 b parent 1
7 b parent 1
8 b child 2
9 c child 2 *
10 c child 3
11 c parent 1
12 c child 2
ie 即
> dt$sequence = 1, wherever dt$member == "parent"
> dt$sequence = previous_row_value + 1, wherever dt$member=="child"
But sometimes it can happen that new ID1 might not start with a member="parent". 但有时可能会发生新的ID1可能无法以member =“parent”开头。 If it starts with "child" (as in example with star-marked row) we have to start sequencing with 2. As of now I have been doing it using loops, like below.
如果以“child”开头(例如星号标记的行),我们必须以2开始排序。到目前为止,我一直在使用循环,如下所示。
dt_sequence <- dt[ ,sequencing(.SD), by="ID1"]
sequencing <- function(dt){
for(i in 1:nrow(dt)){
if(i == 1){
if(dt[i,member] %in% "child")
dt$sequence[i] = 2
else
dt$sequence[i] = 1
}
else{
if(dt[i,member] %in% "child")
dt$sequence[i] = as.numeric(dt$sequence[i-1]) + 1
else
dt$sequence[i] = 1
}
}
return(dt)
}
I ran this code on a data table of 4e5 rows and it took a lot of time to complete (around 20 mins). 我在4e5行的数据表上运行此代码,需要花费大量时间才能完成(大约20分钟)。 Can anyone suggest a faster way to do it.
任何人都可以建议更快的方式来做到这一点。
DF <- read.table(text=" ID1 member
1 a parent
2 a child
3 a parent
4 a child
5 a child
6 b parent
7 b parent
8 b child
9 c child
10 c child
11 c parent
12 c child", header=TRUE, stringsAsFactors=FALSE)
library(data.table)
setDT(DF)
DF[, sequence := seq_along(member) + (member[1] == "child"),
by = list(ID1, cumsum(member == "parent"))]
# ID1 member sequence
# 1: a parent 1
# 2: a child 2
# 3: a parent 1
# 4: a child 2
# 5: a child 3
# 6: b parent 1
# 7: b parent 1
# 8: b child 2
# 9: c child 2
#10: c child 3
#11: c parent 1
#12: c child 2
Try this, 试试这个,
dt$sequence <- rep(NA, length(dt$member))
for (i in seq_along(dt$member)){
dt$sequence[i] <- ifelse(dt$member[i]=="parent", 1,
ifelse(dt$ID1[i]==dt$ID1[i-1], dt$sequence[i-1] + 1, 2)
)
}
and easier dplyr solution 更简单的dplyr解决方案
data <- dt %>%
group_by(ID1) %>%
mutate(
seq = ifelse(member=="parent", 1, 2),
sequence = ifelse(seq==1, 1, lag(seq, default = 1) + 1)
)
If each group ID1
contains at least one parent
, much easier solution will be arranging the data within group=ID1 so that parent
always comes on the top: 如果每个组
ID1
包含至少一个parent
,则更容易的解决方案是在group = ID1中排列数据,以便parent
始终位于顶部:
dt %>%
group_by(ID1) %>%
arrange(desc(member))
Nice question indeed. 确实是个好问题。 So here's my solution:
所以这是我的解决方案:
Data 数据
dd <- structure(list(ID1 = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L),
.Label = c("a", "b", "c"), class = "factor"),
member = structure(c(2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L),
.Label = c("child", "parent"),
class = "factor")),
.Names = c("ID1", "member"),
row.names = c("1", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "11", "12"), class = "data.frame")
Code 码
First, set all elements with parent
to 1: 首先,将
parent
所有元素设置为1:
parent <- dd$member == "parent"
dd$sequence <- 0
dd$sequence[parent] <- 1
Now, set all child
elemetns with no parent to 2: 现在,将所有没有父级的
child
元素设置为2:
dd$sequence <- ave(dd$sequence, dd$ID1,
FUN = function(.) {
ret <- .
ret[1] <- if (ret[1] == 0) 2 else ret[1]
ret}
)
Now, we want to get the length of each sequence of 0's
and the position of each 0
: 现在,我们想要得到的各序列的长度
0's
和每个位置0
:
rl <- rle(dd$sequence)
rl.wh <- which(rl$values == 0)
Finally, we can generate the sequences: 最后,我们可以生成序列:
dd$sequence[dd$sequence == 0] <- unlist(mapply(function(x, r)
seq(x + 1, length.out = r, by = 1), rl$values[rl.wh - 1], rl$length[rl.wh]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.