英文:
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 <- 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
答案1
得分: 2
dplyr
你可以使用 row_number
和 rev
来按组进行操作:
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 %>%
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论