这是我的数据的一小部分样本,请考虑我有超过 100 列
I D M N Score
12 23 11 12 22
11 11 11 11 33
11 11 10 11 44
12 11 12 11 66
11 11 11 12 50
我想从每列中减去分数以获得下表:
Score1 I Score2 D Score3 M Score4 N
10 12 -1 23 12 11 10 12
22 11 22 11 0 11 22 11
33 11 33 11 1 10 33 11
54 12 55 11 -1 12 55 11
39 11 39 11 0 11 38 12
是否有简短的 R 代码来获取此表?
最佳答案
这正是您想要的:
我再次需要社区的帮助 (@akrun) Combining two dataframes with alternating column position
library(dplyr)
df %>%
mutate(across(-Score, ~ Score - ., .names = "Score{1:(ncol(df)-1)}")) %>%
select(-Score) %>%
dplyr::select(all_of(c(matrix(names(.), ncol = ncol(df)-1, byrow = TRUE))))
I Score1 D Score2 M Score3 N Score4
1 12 10 23 -1 11 11 12 10
2 11 22 11 22 11 22 11 22
3 11 33 11 33 10 34 11 33
4 12 54 11 55 12 54 11 55
5 11 39 11 39 11 39 12 38
关于r - 如何对这些数据进行排序以将特定列与 R 中的其他列相减,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72521700/