如何基于R中的三个变量创建索引变量?[duplicate]

3z6pesqy  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(136)
    • 此问题在此处已有答案**:

Numbering rows within groups in a data frame(10个答案)
2天前关闭。
我尝试创建一个基于个人标识符、测试名称测试的日期在R中。我的数据重复了学生多次参加相同的测试,但得分不同。我希望能够轻松地识别每个观察值在该特定测试中的尝试次数。我的数据如下所示,我希望创建一个变量,如图中的ID变量。它应该从1开始,按照日期顺序计算具有相同学生和测试名称的观察数。

student <- c(1,1,1,1,1,1,2,2,2,3,3,3,3,3)
test <-c("math","math","reading","math","reading","reading","reading","math","reading","math","math","math","reading","reading")
date <- c(1,2,3,3,4,5,2,3,5,1,2,3,4,5)
data <- data.frame(student,test,date)
print(data)
   student    test date
1        1    math    1
2        1    math    2
3        1 reading    3
4        1    math    3
5        1 reading    4
6        1 reading    5
7        2 reading    2
8        2    math    3
9        2 reading    5
10       3    math    1
11       3    math    2
12       3    math    3
13       3 reading    4
14       3 reading    5

我想添加一个变量来指示同一个学生参加的测试的尝试次数,这样它看起来就像这样:

student    test date id
1        1    math    1  1
2        1    math    2  2
3        1 reading    3  1
4        1    math    3  3
5        1 reading    4  2
6        1 reading    5  3
7        2 reading    2  1
8        2    math    3  1
9        2 reading    5  2
10       3    math    1  1
11       3    math    2  2
12       3    math    3  3
13       3 reading    4  1
14       3 reading    5  2

我想好了如何仅基于一个变量创建ID变量,例如基于学生号,但我不知道如何为多个变量创建ID变量。我也尝试过cumsum,但它会对每个新值进行计数,并且不会在有新值时从1重新开始。

tests <- transform(tests, ID = as.numeric(factor(EMPLID)))
tests$id <-cumsum(!duplicated(tests[1:3]))
pgky5nke

pgky5nke1#

library(dplyr)
data  %>%
  group_by(student, test) %>%
  arrange(date, .by_group = TRUE) %>%  ## make sure things are sorted by date
  mutate(id = row_number()) %>%
  ungroup()
# # A tibble: 14 × 4
#    student test     date    id
#      <dbl> <chr>   <dbl> <int>
#  1       1 math        1     1
#  2       1 math        2     2
#  3       1 math        3     3
#  4       1 reading     3     1
#  5       1 reading     4     2
#  6       1 reading     5     3
#  7       2 math        3     1
#  8       2 reading     2     1
#  9       2 reading     5     2
# 10       3 math        1     1
# 11       3 math        2     2
# 12       3 math        3     3
# 13       3 reading     4     1
# 14       3 reading     5     2

相关问题