admin管理员组

文章数量:1794759

R语言:if

R语言:if

基本结构展示:

if (7<10) {   print("Seven is less than ten") } else{   print("seven is more than ten") }

 

实例演示:

Titanic=read.csv("goo.gl/4Gqsnz")  #从网络读取数据

1. any()     #any代表只要有任一值符合,即为TRUE

if (any(titanicC$Age>70)) {                                                 print("there are passengers older than 70") } else{   print("no one is older than 70") }

2. all()   #所有都满足才true

if (all(titanicC$Age>10)) {   print("all passengers older than 10") } else{   print("there are passengers younger than 10") }

3. na.omit()        #放的位置决定是删除单一变量缺失值,还是删除任何变量缺失值

if (any(na.omit(titanic$Age==100))) {   print("there are passengers aged 100") } else{   print("there are no passengers aged 100") }                                                                                 #数据库中只要有missing的记录都删掉

if (any(titanic$Age==80, na.rm=TRUE)) {   print("there are passengers aged 80") } else{   print("there are no passengers aged 80") }                                                                               #Age这个变量有missing的记录删掉,其他变量有missing可以保留

4.  else if 写更重复的语句

x=100 y=10 if(x<y){   print("AA") } else if(x==y){   print(BB) } else{   print(CC) }

本文标签: 语言