如何拆分学生标记列表的名称

时间:2017-10-23 19:35:51

标签: r

name=c("John Porter","Anne Theresa","Terry pATRIC","Fred nORTON ","Maria lOPEZ") 
exam1=c(92, 75, 10, 62, 79) 
exam2=c(93,77,49,66,85)

results=data.frame(name, exam1,exam2)     
results$avg=((results$exam1+results$exam2)/2)    
results$grade=cut(results$avg, breaks = c(0,49,69,79,84,100),labels =c("E","D", "C", "B", "A"))     
results

new=strsplit(as.character(results),"")
new    
lastname=sapply(new,"[",2)    
firstname=sapply(new,"[",1)    
results=cbind(firstname,lastname,results[,-1])    
results

results=results[order(lastname,firstname),]    
results

1 个答案:

答案 0 :(得分:0)

定义new变量时,请将results$name转换为字符,而不是results。另外,使用" "(空格)来分割单词,而不是""(没有空格)。如果您修复了这一行,代码似乎可以正常工作。

改变这个:

new=strsplit(as.character(results),"")

到此:

new=strsplit(as.character(results$name)," ")