sapply函数返回空值

问题描述 投票:0回答:1

我正在研究一些基本的R编程,并且进行了一些巧妙的练习,这给我带来了以下问题,我运行了以下代码,但我不明白返回NULL值的原因。

temp <- list(c(3,7,9,6,-1),
         c(6,9,12,13,5),
         c(4,8,3,-1,-3),
         c(1,4,7,2,-2),
         c(5,7,9,4,2),
         c(-3,5,8,9,4),
         c(3,6,9,4,1))

print_info <- function(x) {
  cat("The average temperature is", mean(x), "\n")
}

sapply(temp, print_info)

The average temperature is 4.8 
The average temperature is 9 
The average temperature is 2.2 
The average temperature is 2.4 
The average temperature is 5.4 
The average temperature is 4.6 
The average temperature is 4.6 
NULL
NULL
NULL
NULL
NULL
NULL
NULL

您能帮助我理解为什么我得到这个NULL值吗?

谢谢:)

r function sapply
1个回答
0
投票

这是cat函数的输出:

x = cat('hi\n')
# hi
print(x)
# NULL
© www.soinside.com 2019 - 2024. All rights reserved.