在R中多次打印"Hello world" [英] Printing 'Hello world' n number of times in R

查看:166
本文介绍了在R中多次打印"Hello world"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图自学R,并尝试使用for循环,这是任何命令式语言的基本结构.

I'm trying to teach myself R and am experimenting with for loops, a basic construct in any imperative language.

首先,我使用for循环将变量递增任意次数:

At first, I used a for loop to increment a variable an arbitrary number of times:

x = 0
for (i in 1:1000) {
    x = x + 1
}

这按预期工作.但是,当我尝试在此循环中paste一个简单的消息时,没有任何输出.具体来说:

This works as expected. However, when I attempted to paste a simple message in this loop, I get no output. Specifically:

for (i in 1:1000) {
    paste('Hello world!')
}

这是为什么?

推荐答案

在R中,您需要使用内部循环打印来获取输出:

In R you need to use print inside loops to get the output:

for (i in 1:1000) {
    print(paste('Hello world!'))
}

最好!

这篇关于在R中多次打印"Hello world"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆