gpt4 book ai didi

r - 在每次迭代 lapply 时打印,以便输出在列表元素之间

转载 作者:行者123 更新时间:2023-12-04 10:15:45 25 4
gpt4 key购买 nike

似乎是一个初级的问题。有没有办法到print()message()在函数的迭代之间,以便打印在控制台输出中的列表元素之间。例子:

two_sq <- function (x) {
print(paste("this is ", x))
x^2
}
lapply(1:3, two_sq)

给出:
    # [1] "this is  1"
# [1] "this is 2"
# [1] "this is 3"
# [[1]]
# [1] 1

# [[2]]
# [1] 4

# [[3]]
# [1] 9

所需输出:
# [1] "this is  1"

# # [[1]]
# # [1] 1

# [2] "this is 2"

# # [[2]]
# # [1] 4

# [3] "this is 3"

# # [[3]]
# # [1] 9

最佳答案

two_sq <- function (x) {
print(paste("this is ", x))
print(x^2)
}
invisible(lapply(1:3, two_sq))

# [1] "this is 1"
# [1] 1
# [1] "this is 2"
# [1] 4
# [1] "this is 3"
# [1] 9

关于r - 在每次迭代 lapply 时打印,以便输出在列表元素之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61065670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com