答案 0 :(得分:2)
这是基本的R解决方案
df <- cbind(df,
do.call(rbind,
apply(df[-1], 1, function(v) data.frame(MAX = max(v),Top_Subject = names(v[which.max(v)])))))
这样
> df
Name Math Physics History Chemistry Biology MAX Top_Subject
1 Jack 9 8 7 7 4 9 Math
2 Andy 6 8 6 5 5 8 Physics
3 Peter 7 10 7 6 8 10 Physics
4 Ashley 7 6 5 6 8 8 Biology
数据
df <- structure(list(Name = c("Jack", "Andy", "Peter", "Ashley"), Math = c(9,
6, 7, 7), Physics = c(8, 8, 10, 6), History = c(7, 6, 7, 5),
Chemistry = c(7, 5, 6, 6), Biology = c(4, 5, 8, 8)), class = "data.frame", row.names = c(NA,
-4L))
答案 1 :(得分:0)
基本的R解决方案(以后请c + p dput(df)函数的输出以生成示例而不是图片):
df2 <- transform(df, max_scores = apply(df[,sapply(df, is.numeric)], 1, max),
top_sub_per_stud = names(df[,sapply(df, is.numeric)])[apply(df[,sapply(df, is.numeric)], 1, which.max)])
数据:
df <- structure(list(Name = c("Jack", "Andy", "Peter", "Ashley"), Math = c(9,
6, 7, 7), Physics = c(8, 8, 10, 6), History = c(7, 6, 7, 5),
Chemistry = c(7, 5, 6, 6), Biology = c(4, 5, 8, 8)), class = "data.frame", row.names = c(NA,
-4L))