R语言学习笔记2

##数据的输入
#用键盘输入数据
#在下面的例子中,你将创建一个名为mydata的数据框,它含有三个变量
#age(数值型),gender(字符型),weight(数值型)
#age=numeric(0)的意思是将创建一个指定模式但不含实际数据的变量
mydata<-data.frame(age=numeric(0),gender=character(0),weight=numeric(0))
mydata<-edit(mydata)        

书上例子         

student<-c("John Davis","Angela Williams","Bullwinkle Moose","David Jones
","Janice Markhammer","Cheryl Cushing","Reuven Ytzrhak","Greg knox",
"Joel England","Mary Rayburn"  )
math<-c(502,600,412,358,495,512,410,625,573,522)
science<-c(95,99,80,82,75,85,80,95,89,86)
english<-c(25,22,18,15,20,28,15,30,27,18)
roster<data.frame(student,math,science,english,stringsAsFactors = FALSE)
z<-scale(roster[,2:4])
z
options(digits = 2)
student<-c("John Davis","Angela Williams","Bullwinkle Moose","David Jones
","Janice Markhammer","Cheryl Cushing","Reuven Ytzrhak","Greg knox",
"Joel England","Mary Rayburn"  )
math<-c(502,600,412,358,495,512,410,625,573,522)
science<-c(95,99,80,82,75,85,80,95,89,86)
english<-c(25,22,18,15,20,28,15,30,27,18)
roster<data.frame(student,math,science,english,stringsAsFactors = FALSE)
z<-scale(roster[,2:4])
roster<-data.frame(student,math,science,english,stringsAsFactors = FALSE)
z<-scale(roster[,2:4])
z
#通过计算平均值来得到综合得分
score<-appley(z,1,mean)
score<-apply(z,1,mean)
#将综合得分添加到花名册中
roster<-cbind(roster,score)
roster
y<-quantile(roster$score,c(.8,.6,.4,.2))
y
roster$grade[score>=y[1]]<-"A"
roster$grade[score<y[1]&score>=y[2]]<-"B"
roster$grade[score<y[2]&score>=y[3]]<-"C"
roster$grade[score<y[3]&score>=y[4]]<-"D"
roster$grade[score<y[4]]<-"F"
roster
options(digits = 2)
student<-c("John Davis","Angela Williams","Bullwinkle Moose","David Jones
","Janice Markhammer","Cheryl Cushing","Reuven Ytzrhak","Greg knox",
"Joel England","Mary Rayburn"  )
math<-c(502,600,412,358,495,512,410,625,573,522)
science<-c(95,99,80,82,75,85,80,95,89,86)
english<-c(25,22,18,15,20,28,15,30,27,18)
roster<-data.frame(student,math,science,english,stringsAsFactors = FALSE)
#对数据进行标准化
z<-scale(roster[,2:4])
z
#通过计算平均值来得到综合得分
score<-apply(z,1,mean)
#将综合得分添加到花名册中
roster<-cbind(roster,score)
roster
#通过quantitle()计算学生综合得分的百分位数
y<-quantile(roster$score,c(.8,.6,.4,.2))
y
#通过逻辑运算符将学生百分位数排名编码为一个新的类别型成绩变量
#下面直接在数据框roster中创建了变量garade
roster$grade[score>=y[1]]<-"A"
roster$grade[score<y[1]&score>=y[2]]<-"B"
roster$grade[score<y[2]&score>=y[3]]<-"C"
roster$grade[score<y[3]&score>=y[4]]<-"D"
roster$grade[score<y[4]]<-"F"
roster
name<-strsplit((roster$student)," ")
name
Firstname<-sapply(name,"[",1)
Lastname<-sapply(name,"[",2)
#-1是不要student变量,它在第一列嘛
roster<-cbind(Firstname,Lastname,roster[,-1])
roster
roster(order(Lastname,Firstname),)
roster<-(order(Lastname,Firstname),)
roster<-[order(Lastname,Firstname)]
roster<-[order(Lastname,Firstname)]
#排序
roster[order(Lastname,Firstname)]
roster[order(Lastname,Firstname),]
 

上一篇:JAVA中的反射


下一篇:反射获取类对象