当前位置:网站首页>R语言上课代码记录5
R语言上课代码记录5
2022-04-22 16:42:00 【亭午】
# loop
# For loop
# for (variable in sequence){statement}
# example 1
for(i in c(1:10)) {
print(i)
}
# example 2
A <- letters
for (i in c(1:10)){
cat(A[i],'\n')
}
# example 3
n <- 10
for (i in c(n:1)){
cat(rep('*',i), '\n')
}
# example 4
n <- 5
for (i in c(1:n)){
cat(rep('',n-i+5),rep("*",2*i-1),'\n')
}
# while loop
# while(test_expression){statement}
# if test is TRUE do statement until the test is FALSE
# Example 1
i <- 1
while (i <= 5){
print(i)
i <- i+1
}
i <- 1
while (i <= 5){
cat('The current value',i,'\n')
i <- i+1
}
# Example 2
iteration <- 1
price <- 50
while (price >= 40){
fluctuation <- sample(c(-10:10),1)
price <- price + fluctuation
cat("iteration",iteration,":current stock price is", price, '\n')
iteration <- iteration + 1
}
# Most time, for and while loop can be replaced with each other
# while(condition is TRUE){statement}
# it is equivalent to
# for (i in c(1:10000000)){
# statement
# if(condition is FALSE){break}
# }
# For loop version of while() Example 2
price <- 50
for (i in c(1:10000000)){
fluctuation <- sample(c(-10:10),1)
price <- price + fluctuation
cat("iteration",i,":current stock price is", price, '\n')
if(price < 40){break}
}
# for() loop and which()
id <- c(1:34)
group <- sample(c('A','B','C'), 34, replace=TRUE)
table(group)
score <- sample(c(40:100), 34, replace=TRUE)
mydata <- data.frame(id, group, score)
# use which() to subset data subset子集
# for example, we select only B group students using which()
mydata_B <- mydata[which(mydata$group == 'B'), ]
# use which() to generate a new variable: letter grade
mydata$letter <- NA
mydata$letter[which(mydata$score>=90)] <- 'A'
mydata$letter[which(mydata$score>=80 & mydata$score <90)] <- 'B'
mydata$letter[which(mydata$score>=70 & mydata$score <80)] <- 'C'
mydata$letter[which(mydata$score>=60 & mydata$score <70)] <- 'D'
mydata$letter[which(mydata$score<60)] <- 'F'
# use for() loop to do the same thing
mydata2 <- data.frame(id, group, score)
mydata2$letter <- NA
for (i in 1:nrow(mydata2)){ # nrow() gives you the number of rows
if (mydata2$score[i]>90){mydata2$letter[i] <- 'A'}
else if (mydata2$score[i]>=80 & mydata2$score[i]<90){mydata2$letter[i] <- 'B'}
else if (mydata2$score[i]>=70 & mydata2$score[i]<80){mydata2$letter[i] <- 'C'}
else if (mydata2$score[i]>=60 & mydata2$score[i]<70){mydata2$letter[i] <- 'D'}
else if (mydata2$score[i]<60){mydata2$letter[i] <- 'F'}
}
版权声明
本文为[亭午]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_59179752/article/details/124316145
边栏推荐
猜你喜欢

对于现阶段GameFi发展而言,兼容EVM重要吗?

Crashsight general function & feature function introduction

Unittest - unit test 2

NFT 平台安全指南

This API hub is powerful. It contains open APIs such as nailing enterprise wechat, and can be debugged directly!

Use of serial port data plot serialplot

C ODBC loads the files of one folder into the blob column of Oracle database and downloads the blob column to another folder

Gome's new actions "true selection" and "strict selection" enable multi-dimensional escort quality consumption

matplotlib绘图

Explain JVM memory model in detail
随机推荐
Experiment 4: KNN, naive Bayes of data mining
Facing the global market, platefarm today logs in to four major global platforms such as Huobi
How to select one of the two lines that coincide in SolidWorks
Detailed data of db107-asemi rectifier bridge
Solidity: contract structure
Blue Bridge Cup practice 013
In 2023, Wuhan University of technology energy and power (085800) took the postgraduate entrance examination and landed. Experience guidance of predecessors in preparing for the examination
Scrapy框架进阶学习
Blue Bridge Cup practice 020
An analysis and treatment of abnormal growth of Oracle database table space
RT-Thread Studio 工作区编码设置为UTF-8
DB107-ASEMI整流桥详细数据
Network public opinion monitoring system
【周报】4月17日
Calculation method of numerator and denominator of system driven by stepping servo motor for rotation angle control
Huawei machine test question hj56 complete number calculation
FRP reverse proxy
Insist on doing the right thing
最新流程引擎 flowable 6.7.2 更新说明
Ten thousand yuan gift pool! Play "Lighthouse" award-winning essay attack