我有一个数据集,我想对平均准确率(meanacc)和平均RT (meanrt)进行排名,以找到每个参与者(id)的最佳和最差素数。
数据:
data <- dput(sum_dat_id2)
structure(list(id = structure(c(1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L,
4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L,
9L, 9L), .Label = c("8c2ybacs", "a5y79isc", "amya7xh9", "e9jjib8j",
"gy2iobr9", "i9la2x3h", "lotedm1d", "uhxghpam", "wmke4pwa"), class = "factor"),
Grade = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = "7th", class = "factor"), GradeNum = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "7", class = "factor"),
Prime = structure(c(2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L), .Label = c("s100w100", "s135w150", "s50w100"), class = "factor"),
Sex = c("Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female"), Number_Languages = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("monolingual",
"trilingual"), class = "factor"), meanacc = c(0.5, 0.333333333333333,
0.5, 0.447368421052632, 0.323529411764706, 0.488372093023256,
0.545454545454545, 0.545454545454545, 0.6, 0.5, 0.416666666666667,
0.568181818181818, 0.454545454545455, 0.558139534883721,
0.564102564102564, 0.432432432432432, 0.457142857142857,
0.529411764705882, 0.318181818181818, 0.318181818181818,
0.422222222222222, 0.51219512195122, 0.425, 0.837209302325581,
0.780487804878049, 0.871794871794872), meanrt = c(235.635,
221.1, 218.365416666667, 128.225789473684, 133.196764706176,
914.898255813953, 890.709886363636, 712.409090909091, 282.70525,
444.358088235294, 316.470138888889, 880.698295454545, 720.212954545455,
902.265930232558, 538.900256410256, 444.451081081081, 437.215285714286,
285.566617647059, 105.416363636364, 202.969545454545, 1005.48833333333,
642.993902439024, 760.722, 637.837906976512, 722.863170732439,
656.063461538462), n = c(2L, 3L, 36L, 38L, 34L, 43L, 44L,
44L, 40L, 34L, 36L, 44L, 44L, 43L, 39L, 37L, 35L, 34L, 22L,
22L, 45L, 41L, 40L, 43L, 41L, 39L)), row.names = c(NA, -26L
), groups = structure(list(id = structure(c(1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L,
8L, 8L, 9L, 9L, 9L), .Label = c("8c2ybacs", "a5y79isc", "amya7xh9",
"e9jjib8j", "gy2iobr9", "i9la2x3h", "lotedm1d", "uhxghpam", "wmke4pwa"
), class = "factor"), Grade = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = "7th", class = "factor"), GradeNum = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "7", class = "factor"),
Prime = structure(c(2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L), .Label = c("s100w100", "s135w150", "s50w100"), class = "factor"),
Sex = c("Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female"), .rows = structure(list(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L,
26L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -26L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))当我使用这段代码时,我只得到两列(acc_rank和rt_rank),其中所有内容的排名都是1:
best <-sum_dat_id2 %>% mutate(acc_rank = rank(meanacc, ties.method = "average")) %>%
mutate(rt_rank = rank(meanrt, ties.method = "average")) 结果数据示例:
head(best)
# A tibble: 6 x 11
# Groups: id, Grade, GradeNum, Prime, Sex [6]
id Grade GradeNum Prime Sex Number_Languages meanacc meanrt n acc_rank rt_rank
<fct> <fct> <fct> <fct> <chr> <fct> <dbl> <dbl> <int> <dbl> <dbl>
1 8c2ybacs 7th 7 s135w150 Female monolingual 0.5 236. 2 1 1
2 8c2ybacs 7th 7 s50w100 Female monolingual 0.333 221. 3 1 1
3 a5y79isc 7th 7 s100w100 Female monolingual 0.5 218. 36 1 1
4 a5y79isc 7th 7 s135w150 Female monolingual 0.447 128. 38 1 1
5 a5y79isc 7th 7 s50w100 Female monolingual 0.324 133. 34 1 1
6 amya7xh9 7th 7 s100w100 Female monolingual 0.488 915. 43 1 1有没有人知道为什么会发生这样的事情?
发布于 2021-05-11 11:22:16
您可以在dplyr中使用dense_rank:
library(dplyr)
sum_dat_id2 %>%
group_by (id) %>%
mutate(acc_rank = dense_rank(meanacc),
rt_rank = dense_rank(meanrt)) 发布于 2021-05-12 05:11:52
在data.table中使用frank
library(data.table)
setDT(sum_dat_id2)[, c('acc_rank', 'rt_rank') :=
lapply(.SD, frank, method = 'dense'), id, .SDcols = c('meanacc', 'meanrt')]https://stackoverflow.com/questions/67478608
复制相似问题