R语言for循环用法

 时间:2024-10-16 11:20:14

1、打开R语言或RStudio

R语言for循环用法R语言for循环用法

2、我们想让R语言自动循环5次,每次都输出111,则代码如下:for(i in 1:5) print(111)

R语言for循环用法

3、假如我们想让这5次分别输出1、输出2、输出3...到输出5,则代码如下:for(i in 1:5) print(i)

R语言for循环用法

4、还可以用for循环做其他的事,例如:for(j in 1:5) print(1:j)

R语言for循环用法

5、又如:for(n in c(10,20,30,40,50)) { x <- stats::rnorm(n) cat(n, ": ", sum(x^2), "\n", sep = ";")}

R语言for循环用法

6、又如:f <- factor(sample(letters[6:10], 10, replace = TRUE))for(j in unique(f)) print(j)

R语言for循环用法

7、关于for循环的用法就介绍到这里。

8、大家可以自行尝试哦!

  • r语言读取excel数据
  • 多元线性回归分析的优缺点
  • R语言怎么读取CSV文件
  • R语言怎么把数据转化为矩阵
  • 如何用R语言导入excel文件
  • 热门搜索
    鸡蛋怎么做好吃 智能电视怎么看电视台的频道 华为手机怎么分屏 箸怎么读 冰淇淋怎么做 微信怎么 甲状腺结节怎么引起的 郎平为什么去美国当教练 大头菜的家常做法 刘谦为什么突然不火了