我为5个科目创建了15名学生的分数。我还在每个主题中创建了一个标记向量。我想根据矢量密码中给定的值用“通过”或“失败”来改变标记的每一列...我已经能够手动操作,但是我想使用一些循环功能,以便将密码映射到列上然后进行变异使用“通过”或“失败”创建新列或新数据框。
x <- runif(15)
#convert to DF
marks <- as.data.frame(x)
marks[1:5] <- sapply(1:5, "+", rnorm(5,60,15))
names(marks) <- paste0("sub", 1:5)
marks
colMeans(marks)
lapply(marks, range)
lapply(marks, is.na)
colSums(is.na(marks))
marks1 = round(marks,2)
passmarks = c(60, 65, 62,70, 45)
names(marks1)
marks1 %>% mutate(sub1a = ifelse(sub1 <= passmarks[1], 'F','P'), sub2a = ifelse(sub2 <= passmarks[2], 'F','P'), sub3a = ifelse(sub3 <= passmarks[3], 'F','P'), sub4a = ifelse(sub4 <= passmarks[4], 'F','P'), sub5a = ifelse(sub5 <= passmarks[5], 'F','P'))
marks1 %>% summarise_at(vars(sub1:sub5), mean, na.rm=T)
marks1 %>% mutate_all(funs(./75))
问题的第二部分:::: 问题的这一阶段已解决。我必须为每个学生计算SGPA。如果我有分数,我会给每个学生科目分配分数。这取决于每个主题的平均值和特定序列的标准差。该等级取决于每列标记的均值和所有列均相同的std值。 接下来,如果我们有成绩,我们必须计算获得的总成绩积分。对于每个主题列,都有已知的学分。还已知每个年级的年级分数。我想为每个学生计算学分的数字总和。.我尝试对其中的一部分进行编码...我将寻求您的帮助,以使其变得更简单,更优雅...
#continued
#find mean of each subject
(meanx = colMeans(marks1))
#(sdx=apply(marks1,2,sd))
(sd1 = seq(1.5, -2,-.5)) #this pattern is same across all subjects
#these are the grades associated with each Std Dev
(names(sd1) = c('AP','A','AM','BP','B','BM','CP','C'))
sd1
sd1['AP'] #test for A+
#this is sample vectorise function created: here meanx should be replace with respective mean of the subject
RgradeAssigned = function(x) { ifelse(x >= meanx * sd1['AP'],'AP', ifelse(x >= meanx + sd1['A'], 'A', ifelse(x >= meanx + sd1['AM'], 'AM', ifelse(x >= meanx + sd1['BP'], 'BP', ifelse(x >= meanx * sd1['B'], 'B',ifelse(x >= meanx + sd1['BM'], 'BM',ifelse(x >= meanx + sd1['CP'], 'CP',ifelse(x >= meanx + sd1['C'], 'C', 'F'))))))))}
RgradeAssigned(marks1)
#meanx should mean from the vector meanx : for sub1 it should be meanx[1]
meanx
#Calculate SGPA
#credit for each subject
(subcredits = c(3,2,4,3,3))
#gradepoints with wrt to each grade
gradeName = c('AP','A','AM','BP','B','BM','CP','C','F')
gradePoint = c(10,9,8,7,6,5,4,3,0)
names(gradePoint) = gradeName
gradePoint
# if a particular grade, find grade point -> multiply by subject credit -> for each student -> find sum of grades ie sum each row
# for eg row grade were
#'AP','A','AM','BP','B' -> 10,9,8,7,6 -> 10*3 + 9*2+ 8*4 + 7*3 + 6*3
#subject credits : 3,2,4,3,3
#Now Mapping of Grade to Point is available
答案 0 :(得分:2)
您可以使用mapply
,即
mapply(`<`, passmarks, marks1)
给出,
[,1] [,2] [,3] [,4] [,5] [1,] FALSE FALSE FALSE FALSE TRUE [2,] FALSE FALSE FALSE FALSE TRUE [3,] FALSE FALSE FALSE FALSE TRUE [4,] TRUE TRUE TRUE FALSE TRUE [5,] FALSE FALSE FALSE FALSE TRUE [6,] FALSE FALSE FALSE FALSE TRUE [7,] FALSE FALSE FALSE FALSE TRUE [8,] FALSE FALSE FALSE FALSE TRUE [9,] TRUE TRUE TRUE FALSE TRUE [10,] FALSE FALSE FALSE FALSE TRUE [11,] FALSE FALSE FALSE FALSE TRUE [12,] FALSE FALSE FALSE FALSE TRUE [13,] FALSE FALSE FALSE FALSE TRUE [14,] TRUE FALSE TRUE FALSE TRUE [15,] FALSE FALSE FALSE FALSE TRUE
要获得具有PASS
和FAIL
的矩阵,可以采用的一种方法是
matrix(c('FAIL', 'PASS')[mapply(`<`, passmarks, marks1) + 1], ncol = ncol(marks1))