• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

R语言--一整套数据处理流程及简单的控制流

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
rm(list = ls())
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)
roster[['science']]
roster['science']

z <- scale(roster[,2:4]);z   
score <- apply(z,1,mean);score  #按行求均数
roster <- cbind(roster,score);roster
y <- quantile(score,c(.8,.6,.4,.2));y #求对应百分位点对应的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"
name<-strsplit(roster$Student," ");name #list
name[[3]]
Lastnames <- sapply(name,"[",2);Lastnames   # "[" a function that extracts part of an object
Firstnames <- sapply(name,"[",1)     
roster[,-1]
roster <- cbind(Firstnames,Lastnames,roster[,-1])  #[,-1] 
roster <- roster[order(Lastnames,Firstnames),]
roster

for (i in 1:10) print("Hello")
for(i in c(2,5,10,20,50)) {
  x <- rnorm(i) ;         
  cat(i, ": " ,"\\",sum(x^2), "\n", sep = " ","\a")
}


while (condition) { statament}
i<-10
while(i>0){
  print("hello")  
  i<-i-1}  
grade<- as.character(c("2"));grade
if (is.character(grade)) grade <- as.factor(grade)
if(!is.character(grade)) grade <- as.factor( grade) else print("grade already is a factor")
outcome <- ifelse(score > 0.5 ,print("Passed"),print("Failed"))
if(score > 0.5) print("P") else print("F")
a<- while (roster$score > 0.5) {print("p")
}

type <- c('a') 
switch(EXPR = type, a = 1, b = 2:3,"loist")
ccc <- c("b","QQ","a","A","bb")
for(ch in ccc)      # switch expr  a= (if a in expr),b= (if b in expr),c(else)
  cat(ch,":", switch(EXPR = ch, a = 1, b = 2:3,"loist"), "\n")  #
for(ch in ccc) 
  cat(ch,":", switch(EXPR = ch, A = 1, b = 2:3, "Otherwise: last"),"\n")

  

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap