我是R的新手,我想请教您以下主题的帮助。 很抱歉,我实现了该示例,但数据集大致相似,但有更多的列和行。
statistic total
# Subject Min Median Max # row.names Art English ...
#1 Art 20 59 70 #1 James 59 61
#2 English 30 55 65 #2 Lloyd 60 55
... ...
比较(统计表,中位数)更多(>)比(总表,每个主题)为TRUE(1)或FALSE(0)
output
# names Art English
#1 James 0 1
#2 Lloyld 1 0
提前致谢!
答案 0 :(得分:1)
假设order
中的Subject
statistic
与total
output <- total
nm1 <- setdiff(colnames(total), "row.names")
output[,nm1] <-(total[,nm1] >statistic$Median[col(total[,nm1])])+0
output
# row.names Art English
#1 James 0 1
#2 Lloyd 1 0
如果NA
数据集中有total
个值,例如
total1 <- total
total1$English[1] <- NA
output1 <- total1
output1[,nm1] <- (total1[,nm1] > statistic$Median[col(total[,nm1])])+0
output1[is.na(output1)] <- 0
output1
# row.names Art English
#1 1 0 0
#2 1 1 0
total <- structure(list(row.names = c("James", "Lloyd"), Art = 59:60,
English = c(61L, 55L)), .Names = c("row.names", "Art", "English"
), class = "data.frame", row.names = c("1", "2"))
statistic <- structure(list(Subject = c("Art", "English"), Min = c(20L, 30L
), Median = c(59L, 55L), Max = c(70L, 65L)), .Names = c("Subject",
"Min", "Median", "Max"), class = "data.frame", row.names = c("1",
"2"))