当前位置:网站首页>19 r judgment control function exercise
19 r judgment control function exercise
2022-06-10 07:00:00 【THE ORDER】
> x=rnorm(1,3,1)
> if(x>3) y=1 else y=0
> y
[1] 1
> ifelse(rbinom(10,1,0.5)," word "," flowers ")
[1] " flowers " " word " " flowers " " word " " flowers " " word " " word " " flowers " " word " " flowers "
> set.seed(1)
> x=c("A","B","C","A","D")
> ifelse(x=="C"," no ",ifelse(x=="D",NA," yes "))# Can only replace
[1] " yes " " yes " " no " " yes " NA
> for (i in x) {# Traversal substitution x Elements in
+ j=match(i,x)# Return to subscript position
+ x1=switch(i,"A"=" yes ","B"=" yes ","C"=" no ","D"="NA")
+ x[j]=x1
+
+ }
> x
[1] " yes " " yes " " no " " yes " "NA"
> for (i in letters[1:5]) {
+ print(i)
+ }
[1] "a"
[1] "b"
[1] "c"
[1] "d"
[1] "e"
> m=matrix(1:6,2,3)
> m
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
> for(i in 1:2){
+ for(j in 1:3){
+ print(m[i,j])
+ }
+ }
[1] 1
[1] 3
[1] 5
[1] 2
[1] 4
[1] 6
> j=1
> count=0
> while(count<6){
+ print(count)
+ count=count+1
+ }
[1] 0
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
> x=5
> while(x>=3 &x<10){
+ print(x)
+ x=ifelse(rbinom(10,1,0.5),x+1,x+0)
+
+ }
[1] 5
[1] 5 5 6 6 5 6 6 6 6 5
[1] 5 5 7 6 6 6 7 7 6 6
[1] 6 5 8 6 6 6 7 7 7 6
[1] 6 6 8 6 7 7 8 7 8 6
[1] 7 7 9 7 8 8 8 7 9 7
[1] 7 8 9 7 8 8 8 8 10 7
[1] 8 8 9 7 9 8 8 9 10 8
[1] 8 9 9 7 9 9 9 9 11 9
[1] 8 10 9 7 10 9 10 9 11 9
[1] 8 10 10 8 11 10 10 9 12 10
[1] 9 10 10 9 12 10 10 9 13 11
There were 12 warnings (use warnings() to see them)
> for (i in 1:20) {
+ if(i<10) next
+ print(i)
+ }
[1] 10
[1] 11
[1] 12
[1] 13
[1] 14
[1] 15
[1] 16
[1] 17
[1] 18
[1] 19
[1] 20
> loan=c("A","B","D","C","C","B","A","B","A","D")
> ifelse(loan=="D"," no ",ifelse(loan=="B","NA"," yes "))# Vector judgment substitution
[1] " yes " "NA" " no " " yes " " yes " "NA" " yes " "NA" " yes " " no "
> m=matrix(1:81,9,9)
> for (i in 1:9) {
+ x=1:9
+ m[i,]=x*i
+ }
> m
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 1 2 3 4 5 6 7 8 9
[2,] 2 4 6 8 10 12 14 16 18
[3,] 3 6 9 12 15 18 21 24 27
[4,] 4 8 12 16 20 24 28 32 36
[5,] 5 10 15 20 25 30 35 40 45
[6,] 6 12 18 24 30 36 42 48 54
[7,] 7 14 21 28 35 42 49 56 63
[8,] 8 16 24 32 40 48 56 64 72
[9,] 9 18 27 36 45 54 63 72 81
> x=as.matrix(1:9)
> y=t(as.matrix(1:9))
> m=x%*%y
> m
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 1 2 3 4 5 6 7 8 9
[2,] 2 4 6 8 10 12 14 16 18
[3,] 3 6 9 12 15 18 21 24 27
[4,] 4 8 12 16 20 24 28 32 36
[5,] 5 10 15 20 25 30 35 40 45
[6,] 6 12 18 24 30 36 42 48 54
[7,] 7 14 21 28 35 42 49 56 63
[8,] 8 16 24 32 40 48 56 64 72
[9,] 9 18 27 36 45 54 63 72 81
> 1:9 %*%t(1:9) # The forced conversion became 1:9 Matrix , Matrix multiplication %*%
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 1 2 3 4 5 6 7 8 9
[2,] 2 4 6 8 10 12 14 16 18
[3,] 3 6 9 12 15 18 21 24 27
[4,] 4 8 12 16 20 24 28 32 36
[5,] 5 10 15 20 25 30 35 40 45
[6,] 6 12 18 24 30 36 42 48 54
[7,] 7 14 21 28 35 42 49 56 63
[8,] 8 16 24 32 40 48 56 64 72
[9,] 9 18 27 36 45 54 63 72 81
> ?print
> for (i in 1:9) {#print Do not wrap , Haven't found a way , use cat Can achieve print Vertical line printing ,cat Horizontal print
+ for (j in 1:i) {
+ x=i*j
+ #a=paste(c(i,"*",j,"=",x," "),collapse="")
+ cat(i,"*",j,"=",x," ")
+ }
+ cat("\n")
+ }
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
边栏推荐
- Typecho模板 vCards/简约个性便的个人博客主题模板
- How to use Navicat to create an associated primary foreign key for two tables
- 【宽度优先搜索】LeetCode1091二进制矩阵中的最短路径
- 27. encapsulate an animation function
- 2022-06-09:每个会议给定开始和结束时间, 后面的会议如果跟前面的会议有任何冲突,完全取消冲突的、之前的会议,安排当前的。 给定一个会议数组,返回安排的会议列表。 来自通维数码。
- Geek Daily: Huawei said it would not withdraw from the overseas market; NVIDIA confirmed that it was attacked by hackers; Typescript 4.6 release geek headlines
- 梦笔记0610
- RT-Thread设计与实现:RT-Thread 概述和架构
- Baidu cloud address identification
- PHP implementation keeps two decimal places
猜你喜欢
![[dynamic programming] Game Theory: a collection of stone games](/img/ae/98e0fe741ed6c90e83cba244392c88.png)
[dynamic programming] Game Theory: a collection of stone games

LabVIEW controls Arduino to realize infrared ranging (advanced chapter-6)

晨曦记账本记账,使用项目查看账目

ShardingSphere实践(6)——弹性伸缩

Principe de l'algorithme d'extraction de l'ensemble d'éléments fréquents associés à l'alarme dans le cadre de l'exploitation et de l'entretien intelligents

如何快速剪辑多个短视频,去除视频片尾

Read in one second: the practical operation of increasing capital and shares of enterprises!

Opengauss database ODBC environment connection configuration (Windows)

Matlab: polynomial representation and its basic operations

29. solution for 300ms delay time of click event at mobile terminal
随机推荐
What's new in openlayers 6.13
32-bit microcontroller
如何快速剪辑多个短视频,去除视频片尾
OpenCV学习(二)---树莓派上安装opencv
bson,json
Daily practice: Longest symmetric string (pay attention to the last two methods)
30. localstorage and sessionstorage are unknown
Read in one second: the practical operation of increasing capital and shares of enterprises!
3DMAX modeling scenario
LabVIEW controls Arduino to realize infrared ranging (advanced chapter-6)
一本通1281.最长上升子序列 题解 动态规划
29. solution for 300ms delay time of click event at mobile terminal
2022-2027 (New Edition) forecast report on application status and development potential of China's superhard material industry
3DMAX Dragon
GO+VUE+PGSQL-家族管理系统项目结项
创建RT-thread软件仿真工程 写RT-thread内核
解析:稳定币不是“稳定的币”,其本质是一种产品
白加黑过360启动项工具源码发布 多文件过启动项代码
Teleyecontroller v8.16 release complete registry function
Wechat team sharing: how the wechat background does not crash under massive concurrent requests