admin管理员组文章数量:1794759
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) }
本文标签: 语言
版权声明:本文标题:R语言:if 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686491115a73460.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论