根据ID反转列

huangapple go评论63阅读模式
英文:

Reverse Column based on ID

问题

我有一个包含一些重复ID的数据集(ID的频率不同)。还有一列显示了每个ID的第n次出现。我试图找到一种快速的方法来为每个ID创建一个新列,该列将反转该时间列。例如,对于以下向量,我希望它们看起来像这样:

(1, 1, 1, 2, 3, 3, 3, 3, 3) = id
(1, 2, 3, 1, 1, 2, 3, 4, 5) = time
(3, 2, 1, 1, 5, 4, 3, 2, 1) = time_reverse

我在下面提供了一个可重现的示例:

df <- data.frame(
  id = rep(c(1, 2, 3, 4, 5), times = c(3, 1, 5, 2, 7)),
  time = c(1, 2, 3, 1, 1, 2, 3, 4, 5, 1, 2, 1, 2, 3, 4, 5, 6, 7), 
  time_reverse = sample(NA, size = 18, replace = TRUE)
)

谢谢!

英文:

I have a dataset with some recurring IDs (there are different frequencies of IDs). There also is a column which shows the nth occurrence of each ID.
I am trying to find a quick way to create a new column which will reverse that time column for each ID.
For example for the following vectors i would expect them to look like this:

(1, 1, 1, 2, 3, 3, 3, 3, 3) = id  
(1, 2, 3, 1, 1, 2, 3, 4, 5) = time  
(3, 2, 1, 1, 5, 4, 3, 2, 1) = time_reverse

I posted a reproducible example below.

df &lt;- data.frame(
  id = rep(c(1, 2, 3, 4, 5), times = c(3, 1, 5, 2, 7)),
  time = c(1, 2, 3, 1, 1, 2,  3, 4, 5, 1, 2, 1, 2, 3, 4, 5, 6, 7), 
  time_reverse = sample(NA, size = 18, replace = TRUE)
)

Thanks in advance 根据ID反转列

答案1

得分: 2

dplyr

你可以使用 row_numberrev 来按组进行操作:

library(dplyr)
df %>%
  group_by(id) %>%
  mutate(time = row_number(), 
         time_reverse = rev(time))

base R

transform(df,
          time = ave(id, id, FUN = seq_along),
          time_reverse = ave(time, id, FUN = rev))
      id  time time_reverse
 1     1     1            3
 2     1     2            2
 3     1     3            1
 4     2     1            1
 5     3     1            5
 6     3     2            4
 7     3     3            3
 8     3     4            2
 9     3     5            1
10     4     1            2
11     4     2            1
12     5     1            7
13     5     2            6
14     5     3            5
15     5     4            4
16     5     5            3
17     5     6            2
18     5     7            1
英文:

dplyr

You can use row_number and rev by group:

library(dplyr)
df %&gt;% 
  group_by(id) %&gt;% 
  mutate(time = row_number(), 
         time_reverse = rev(time))

base R

transform(df,
          time = ave(id, id, FUN = seq_along),
          time_reverse = ave(time, id, FUN = rev))
      id  time time_reverse
 1     1     1            3
 2     1     2            2
 3     1     3            1
 4     2     1            1
 5     3     1            5
 6     3     2            4
 7     3     3            3
 8     3     4            2
 9     3     5            1
10     4     1            2
11     4     2            1
12     5     1            7
13     5     2            6
14     5     3            5
15     5     4            4
16     5     5            3
17     5     6            2
18     5     7            1

huangapple
  • 本文由 发表于 2023年3月7日 17:41:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定