R:从每个组的第一行减去行

发布于 2025-02-05 05:44:59 字数 562 浏览 1 评论 0原文

我想从列距离中的每个组中的第一行减去每个行。

我的数据如下:

structure(list(Group = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L), Distance = c(0.05, 0.084, 0.06, 0.03, 
0.034, 0.0534, 0.034, 0.23, 0.34, 0.6435, 0.6346, 0.234, 0.246, 
0.4, 0.7)), class = "data.frame", row.names = c(NA, -15L))

我尝试了以下代码:

Data <- Data %>%
  group_by(Group) %>%
  mutate(Difference=Distance - dplyr:: lag(Distance))

但是,如何调整代码,以使每个组的列中的值与每个组的第一行中的值减去列距离中的值? 因此,对于第1组而言,这意味着值0.084、0.06、0.03和0.034都是从0.05中减去的。

I would like to subtract each rows from the first row in each Group for the column Distance.

My data is the following:

structure(list(Group = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L), Distance = c(0.05, 0.084, 0.06, 0.03, 
0.034, 0.0534, 0.034, 0.23, 0.34, 0.6435, 0.6346, 0.234, 0.246, 
0.4, 0.7)), class = "data.frame", row.names = c(NA, -15L))

I have tried the following code:

Data <- Data %>%
  group_by(Group) %>%
  mutate(Difference=Distance - dplyr:: lag(Distance))

However, how can I adjust the code so that for each Group the value in column Distance is subtracted from the value in the first row for each group?
So for group 1 it would mean that the values 0.084, 0.06, 0.03 and 0.034 are all subtracted from 0.05.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惟欲睡 2025-02-12 05:45:00

更新:请参阅评论:
非常感谢@gregor Thomas:

library(dplyr)
Data %>% 
  group_by(Group) %>% 
  mutate(Distance = Distance - Distance[1])

好的,现在我也看到了。它与dplyr方式的评论中提供的格雷戈尔·托马斯(Gregor Thomas)是相同的解决方案!

Group Distance
   <int>    <dbl>
 1     1   0     
 2     1   0.034 
 3     1   0.0100
 4     1  -0.02  
 5     1  -0.016 
 6     2   0     
 7     2  -0.0194
 8     2   0.177 
 9     2   0.287 
10     2   0.590 
11     3   0     
12     3  -0.401 
13     3  -0.389 
14     3  -0.235 
15     3   0.0654

第一个答案:
这是另一个dplyr选项:

library(dplyr)
Data %>% 
  group_by(Group) %>% 
  mutate(Distance = Distance - Distance[row_number() == 1])
   Group Distance
   <int>    <dbl>
 1     1   0     
 2     1   0.034 
 3     1   0.0100
 4     1  -0.02  
 5     1  -0.016 
 6     2   0     
 7     2  -0.0194
 8     2   0.177 
 9     2   0.287 
10     2   0.590 
11     3   0     
12     3  -0.401 
13     3  -0.389 
14     3  -0.235 
15     3   0.0654

Update: see comments:
Many thanks to @Gregor Thomas:

library(dplyr)
Data %>% 
  group_by(Group) %>% 
  mutate(Distance = Distance - Distance[1])

Ok now I see it also. It is the same solution as Gregor Thomas provided in the comments just in dplyr way!

Group Distance
   <int>    <dbl>
 1     1   0     
 2     1   0.034 
 3     1   0.0100
 4     1  -0.02  
 5     1  -0.016 
 6     2   0     
 7     2  -0.0194
 8     2   0.177 
 9     2   0.287 
10     2   0.590 
11     3   0     
12     3  -0.401 
13     3  -0.389 
14     3  -0.235 
15     3   0.0654

First answer:
Here is another dplyr option:

library(dplyr)
Data %>% 
  group_by(Group) %>% 
  mutate(Distance = Distance - Distance[row_number() == 1])
   Group Distance
   <int>    <dbl>
 1     1   0     
 2     1   0.034 
 3     1   0.0100
 4     1  -0.02  
 5     1  -0.016 
 6     2   0     
 7     2  -0.0194
 8     2   0.177 
 9     2   0.287 
10     2   0.590 
11     3   0     
12     3  -0.401 
13     3  -0.389 
14     3  -0.235 
15     3   0.0654
紧拥背影 2025-02-12 05:45:00

一个可能的解决方案:

library(dplyr)

df %>% 
  group_by(Group) %>% 
  mutate(Distance2 = first(Distance) - Distance) %>% 
  ungroup

#> # A tibble: 15 x 3
#>    Group Distance Distance2
#>    <int>    <dbl>     <dbl>
#>  1     1   0.05      0     
#>  2     1   0.084    -0.034 
#>  3     1   0.06     -0.0100
#>  4     1   0.03      0.02  
#>  5     1   0.034     0.016 
#>  6     2   0.0534    0     
#>  7     2   0.034     0.0194
#>  8     2   0.23     -0.177 
#>  9     2   0.34     -0.287 
#> 10     2   0.644    -0.590 
#> 11     3   0.635     0     
#> 12     3   0.234     0.401 
#> 13     3   0.246     0.389 
#> 14     3   0.4       0.235 
#> 15     3   0.7      -0.0654

A possible solution:

library(dplyr)

df %>% 
  group_by(Group) %>% 
  mutate(Distance2 = first(Distance) - Distance) %>% 
  ungroup

#> # A tibble: 15 x 3
#>    Group Distance Distance2
#>    <int>    <dbl>     <dbl>
#>  1     1   0.05      0     
#>  2     1   0.084    -0.034 
#>  3     1   0.06     -0.0100
#>  4     1   0.03      0.02  
#>  5     1   0.034     0.016 
#>  6     2   0.0534    0     
#>  7     2   0.034     0.0194
#>  8     2   0.23     -0.177 
#>  9     2   0.34     -0.287 
#> 10     2   0.644    -0.590 
#> 11     3   0.635     0     
#> 12     3   0.234     0.401 
#> 13     3   0.246     0.389 
#> 14     3   0.4       0.235 
#> 15     3   0.7      -0.0654
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文