用不同的值替换组中的最后一个值 [英] Replacing the last value within groups with different values

查看:179
本文介绍了用不同的值替换组中的最后一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于此帖子,但区别是不是用每个组/ id中的最后一个值替换为全0,不同的值用于替换每个组/ id中的最后一个值。



这是一个例子(我从上面的链接借用它):

  id时间
1 1 3
2 1 10
3 1 1
4 1 0
5 1 9999
6 2 0
7 2 9
8 2 500
9 3 0
10 3 1


$ b b

在上面的链接中,每个组/ id中的最后一个值被替换为零,如下所示:

  df%>%
group_by(id)%>%
mutate(Time = c(Time [-n()],0))

输出为

  id Time 
1 1 3
2 1 10
3 1 1
4 1 0
5 1 0
6 2 0
7 2 9
8 2 0
9 3 0
10 3 0

,我想要每个组/ id中的最后一个值替换为不同的值。最初,每个组/ id中的最后一个值为 9999 500 1 。现在我想: 9999 替换为 5 500 替换为 12 1 替换为 92 。所需输出为:

  id时间
1 1 3
2 1 10
3 1 1
4 1 0
5 1 5
6 2 0
7 2 9
8 2 12
9 3 0
10 3 92

我试过这个:

  df%>%
group_by(id)%>%
mutate(Time = replace(Time,n(),c(5,12,92) )),

但不起作用。

另一种使用 data.table 的方法是创建另一个 data.table 要替换为给定 id 的值,然后加入并通过引用更新(同时)。

  require(data.table)#v1.9.5 +(对于'on ='特性)
replace = data.table(id = val = c(5L,12L,9L))#from @David
setDT(df)[replace,Time:= val,on =id,mult =last]
$ b b#id时间
#1:1 3
#2:1 10
#3:1 1
#4:1 0
#5:1 5
#6:2 0
#7:2 9
#8:2 12
#9:3 0
#10:3 9




data.table joins 被视为子集的扩展。我们自然会想到在连接上做 的任何操作。


对于每个 replace $ id ,我们在 df $ id 中找到最后匹配的行( mult =last更新该行与相应的 val



v1.9.5 的安装说明这里。希望这有帮助。


My question is similar to this post, but the difference is instead of replacing the last value within each group/id with all 0's, different values are used to replace the last value within each group/id.

Here is an example (I borrowed it from the above link):

          id  Time
1         1    3
2         1    10
3         1    1
4         1    0
5         1    9999
6         2    0
7         2    9
8         2    500
9         3    0
10        3    1

In the above link, the last value within each group/id was replaced by a zero, using something like:

df %>%
  group_by(id) %>%
  mutate(Time = c(Time[-n()], 0))

And the output was

          id  Time
1         1    3
2         1    10
3         1    1
4         1    0
5         1    0
6         2    0
7         2    9
8         2    0
9         3    0
10        3    0

In my case, I would like the last value within each group/id to be replaced by a different value. Originally, the last value within each group/id was 9999, 500, and 1. Now I would like: 9999 is replaced by 5, 500 is replaced by 12, and 1 is replaced by 92. The desired output is:

          id  Time
1         1    3
2         1    10
3         1    1
4         1    0
5         1    5
6         2    0
7         2    9
8         2    12
9         3    0
10        3    92

I tried this one:

df %>%
  group_by(id) %>%
  mutate(Time = replace(Time, n(), c(5,12,92))),

but it did not work.

解决方案

Another way using data.table would be to create another data.table which contains the values to be replaced with for a given id, and then join and update by reference (simultaneously).

require(data.table) # v1.9.5+ (for 'on = ' feature)
replace = data.table(id = 1:3, val = c(5L, 12L, 9L)) # from @David
setDT(df)[replace, Time := val, on = "id", mult = "last"]

#     id Time
#  1:  1    3
#  2:  1   10
#  3:  1    1
#  4:  1    0
#  5:  1    5
#  6:  2    0
#  7:  2    9
#  8:  2   12
#  9:  3    0
# 10:  3    9

In data.table, joins are considered as an extension of subsets. It's natural to think of doing whatever operation we do on subsets also on joins. Both operations do something on some rows.

For each replace$id, we find the last matching row (mult = "last") in df$id, and update that row with the corresponding val.

Installation instructions for v1.9.5 here. Hope this helps.

这篇关于用不同的值替换组中的最后一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆