R data.table根据唯一ID的第一行和最后一行生成具有间隔的序列
我有一个这样的数据表。如何为ID号中具有相同时间的每一行生成一个.25步的序列。我对R很陌生,尝试做一些数据争论R data.table根据唯一ID的第一行和最后一行生成具有间隔的序列,r,data.table,sequence,R,Data.table,Sequence,我有一个这样的数据表。如何为ID号中具有相同时间的每一行生成一个.25步的序列。我对R很陌生,尝试做一些数据争论 id time 1: 1 14 2: 1 14 3: 1 14 4: 1 14 5: 1 18 6: 1 18 7: 1 22 8: 1 22 9: 2 8 10: 2 8 11: 2 8 12: 2 8 13: 2 12 14: 2 15 15: 2 15
id time
1: 1 14
2: 1 14
3: 1 14
4: 1 14
5: 1 18
6: 1 18
7: 1 22
8: 1 22
9: 2 8
10: 2 8
11: 2 8
12: 2 8
13: 2 12
14: 2 15
15: 2 15
16: 2 15
17: 2 19
18: 2 19
19: 2 19
20: 2 19
我希望是这样
id time new_time
1: 1 14 14.00
2: 1 14 14.25
3: 1 14 14.50
4: 1 14 14.75
5: 1 18 18.00
6: 1 18 18.25
7: 1 22 22.00
8: 1 22 22.25
9: 2 8 8.00
10: 2 8 8.25
11: 2 8 8.50
12: 2 8 8.75
13: 2 12 12.00
14: 2 15 15.00
15: 2 15 15.25
16: 2 15 15.50
17: 2 19 19.00
18: 2 19 19.25
19: 2 19 19.50
20: 2 19 19.75
您可以使用
seq
中的length.out
参数,我们将该参数设置为每个时间的组大小(即下面代码中的.N
,它是数据提供的一个特殊符号。
表提供,请参见?.N
)
另一个选项是rowid
(和一些演算)
数据
library(data.table)
DT <- fread(text = " id time
1 14
1 14
1 14
1 14
1 18
1 18
1 22
1 22
2 8
2 8
2 8
2 8
2 12
2 15
2 15
2 15
2 19
2 19
2 19
2 19")
库(data.table)
DT考虑下次制作一个可重复的示例。(参见我在代码中给出的示例,这将在将来遇到问题时有所帮助)
我使用tidyverse(特别是dplyr包)来解决这个问题
## Load library (this loads lots of packages, specifically we are using dplyr)
library(tidyverse)
## Reproducible example
data <- tibble(id = c(rep(1,8),rep(2,12)),
time = c(rep(14,4),rep(18,2),rep(22,2),rep(8,4),12,rep(15,3),rep(19,4)))
print(data)
# A tibble: 20 x 2
id time
<dbl> <dbl>
1 1 14
2 1 14
3 1 14
4 1 14
5 1 18
6 1 18
7 1 22
8 1 22
9 2 8
10 2 8
11 2 8
12 2 8
13 2 12
14 2 15
15 2 15
16 2 15
17 2 19
18 2 19
19 2 19
20 2 19
## Data with increments for each group
new_data <- data %>%
##Groups your data by the same variable, in this case you want to increment by 0.25 for each id within the time group
group_by(time) %>%
## Increments each id by 0.25
mutate(new_time = ifelse((row_number() == 1), time, (0.25 * (row_number()-1)) + time)) %>%
## Ungroups the data
ungroup()
print(as.data.frame(new_data))
id time new_time
1 1 14 14.00
2 1 14 14.25
3 1 14 14.50
4 1 14 14.75
5 1 18 18.00
6 1 18 18.25
7 1 22 22.00
8 1 22 22.25
9 2 8 8.00
10 2 8 8.25
11 2 8 8.50
12 2 8 8.75
13 2 12 12.00
14 2 15 15.00
15 2 15 15.25
16 2 15 15.50
17 2 19 19.00
18 2 19 19.25
19 2 19 19.50
20 2 19 19.75
##加载库(这会加载很多包,特别是我们使用的是dplyr)
图书馆(tidyverse)
##可复制示例
数据%
##每个id的增量为0.25
变异(新时间=ifelse((行数()==1),时间,(0.25*(行数()-1))+时间))%>%
##解组数据
解组()
打印(as.data.frame(新数据))
新时间
1 1 14 14.00
2 1 14 14.25
3 1 14 14.50
4 1 14 14.75
5 1 18 18.00
6 1 18 18.25
7 1 22 22.00
8 1 22 22.25
9 2 8 8.00
10 2 8 8.25
11 2 8 8.50
12 2 8 8.75
13 2 12 12.00
14 2 15 15.00
15 2 15 15.25
16 2 15 15.50
17 2 19 19.00
18 2 19 19.25
19 2 19 19.50
20 2 19 19.75
library(data.table)
DT <- fread(text = " id time
1 14
1 14
1 14
1 14
1 18
1 18
1 22
1 22
2 8
2 8
2 8
2 8
2 12
2 15
2 15
2 15
2 19
2 19
2 19
2 19")
## Load library (this loads lots of packages, specifically we are using dplyr)
library(tidyverse)
## Reproducible example
data <- tibble(id = c(rep(1,8),rep(2,12)),
time = c(rep(14,4),rep(18,2),rep(22,2),rep(8,4),12,rep(15,3),rep(19,4)))
print(data)
# A tibble: 20 x 2
id time
<dbl> <dbl>
1 1 14
2 1 14
3 1 14
4 1 14
5 1 18
6 1 18
7 1 22
8 1 22
9 2 8
10 2 8
11 2 8
12 2 8
13 2 12
14 2 15
15 2 15
16 2 15
17 2 19
18 2 19
19 2 19
20 2 19
## Data with increments for each group
new_data <- data %>%
##Groups your data by the same variable, in this case you want to increment by 0.25 for each id within the time group
group_by(time) %>%
## Increments each id by 0.25
mutate(new_time = ifelse((row_number() == 1), time, (0.25 * (row_number()-1)) + time)) %>%
## Ungroups the data
ungroup()
print(as.data.frame(new_data))
id time new_time
1 1 14 14.00
2 1 14 14.25
3 1 14 14.50
4 1 14 14.75
5 1 18 18.00
6 1 18 18.25
7 1 22 22.00
8 1 22 22.25
9 2 8 8.00
10 2 8 8.25
11 2 8 8.50
12 2 8 8.75
13 2 12 12.00
14 2 15 15.00
15 2 15 15.25
16 2 15 15.50
17 2 19 19.00
18 2 19 19.25
19 2 19 19.50
20 2 19 19.75