在for循环中打印同一行上的多个变量的值

时间:2022-10-25 00:12:02

I have a question that must surely seem very trivial, but the answer has always alluded me: how do you print values for multiple variables on the same line from within a for-loop?

我有一个问题肯定显得非常微不足道,但答案总是让我提到:你如何在for-loop中打印同一行上的多个变量的值?

I present two solutions neither of which relies on nothing more than a formatted print statement, and I am still interested in whether print can, by itself, be used to return output in the desired format.

我提出了两个解决方案,它们都不仅仅依赖于格式化的print语句,而且我仍然感兴趣的是print本身是否可以用于以所需的格式返回输出。

First I present the for-loop, which contains one solution, and then I present a function that represents another solution:

首先,我展示了包含一个解决方案的for循环,然后我提出了一个代表另一个解决方案的函数:

P <- 243.51
t <- 31 / 365
n <- 365

for (r in seq(0.15, 0.22, by = 0.01)) {

     A <- P * ((1 + (r/ n))^ (n * t))
     interest <- A - P

     # this prints each variable on a separate line
     print (r)
     print (interest)

     # this does not work
     # print c(r, interest)

     # this presents both variables on the same line, as desired
     output <- c(r,interest)
     print(output)

     # EDIT - I just realized the line below prints output in the desired format
     print (c(r, interest))

}


# this function also returns output in the desired format

data.fn <- function(r) {

     interest <- P*(1+(r/ n))^(n*t) - P
     list(r = r, interest = interest)

}

my.output <- as.data.frame(data.fn(seq(0.15, 0.22, by = 0.01)))
my.output

#      r interest
# 1 0.15 3.121450
# 2 0.16 3.330918
# 3 0.17 3.540558
# 4 0.18 3.750370
# 5 0.19 3.960355
# 6 0.20 4.170512
# 7 0.21 4.380842
# 8 0.22 4.591345

Is there a way to format print statements in the for-loop so that the print statement, by itself, returns output formatted as in my.output? I know that I could also place a matrix inside the for-loop that stores the values of r and interest and then print the matrix after the loop finished. However, I thought using a print statement would be easier, especially since I do not need to retain the values of r or interest.

有没有办法在for循环中格式化print语句,以便print语句本身返回my.output中格式化的输出?我知道我也可以在for循环中放置一个矩阵来存储r和interest的值,然后在循环结束后打印矩阵。但是,我认为使用print语句会更容易,特别是因为我不需要保留r或者兴趣的值。

Thank you for any advice. Sorry again this question is so trivial. I have searched quite a bit for the answer over an extended period, but have never found the solution. Maybe I have presented enough solutions in this post to make additional possible solutions overkill. Nevertheless, I remain interested.

谢谢你的任何建议。再次抱歉,这个问题非常简单。我在很长一段时间内搜索了相当多的答案,但从未找到解决方案。也许我已经在这篇文章中提出了足够的解决方案,以提供额外的可能解决方案。不过,我仍然感兴趣。

EDIT:

编辑:

In addition to the helpful responses below I just realized that using:

除了下面的有用回复,我才意识到使用:

print (c(r, interest))

in the above for-loop also works.

在上面的for循环中也有效。

2 个解决方案

#1


16  

Try out cat and sprintf in your for loop.

在你的for循环中尝试cat和sprintf。

eg.

例如。

cat(sprintf("\"%f\" \"%f\"\n", df$r, df$interest))

See here

看这里

#2


3  

As an additional note, there is no need for the for loop because of R's vectorization.

另外需要注意的是,由于R的向量化,不需要for循环。

This:

这个:

P <- 243.51
t <- 31 / 365
n <- 365

for (r in seq(0.15, 0.22, by = 0.01))    
     A <- P * ((1 + (r/ n))^ (n * t))
     interest <- A - P
}

is equivalent to:

相当于:

P <- 243.51
t <- 31 / 365
n <- 365
r <- seq(0.15, 0.22, by = 0.01)
A <- P * ((1 + (r/ n))^ (n * t))
interest <- A - P

Because r is a vector, the expression above containing it is performed for all values of the vector.

因为r是向量,所以对于向量的所有值执行包含它的上面的表达式。

#1


16  

Try out cat and sprintf in your for loop.

在你的for循环中尝试cat和sprintf。

eg.

例如。

cat(sprintf("\"%f\" \"%f\"\n", df$r, df$interest))

See here

看这里

#2


3  

As an additional note, there is no need for the for loop because of R's vectorization.

另外需要注意的是,由于R的向量化,不需要for循环。

This:

这个:

P <- 243.51
t <- 31 / 365
n <- 365

for (r in seq(0.15, 0.22, by = 0.01))    
     A <- P * ((1 + (r/ n))^ (n * t))
     interest <- A - P
}

is equivalent to:

相当于:

P <- 243.51
t <- 31 / 365
n <- 365
r <- seq(0.15, 0.22, by = 0.01)
A <- P * ((1 + (r/ n))^ (n * t))
interest <- A - P

Because r is a vector, the expression above containing it is performed for all values of the vector.

因为r是向量,所以对于向量的所有值执行包含它的上面的表达式。