我试图使用cat()作为apply()内的函数.我几乎可以让R做我想做的事,但是在回归结束时我对NULLS感到非常困惑(对我而言).这是一个愚蠢的例子,突出我所得到的.
val1 <- 1:10
val2 <- 25:34
values <- data.frame(val1, val2)
apply(values, 1, function(x) cat(x[1], x[2], fill=TRUE))
Run Code Online (Sandbox Code Playgroud)
这个"有用",因为R接受它并且它运行,但我不理解结果.
> apply(values, 1, function(x) cat(x[1], x[2], fill=TRUE))
1 25
2 26
3 27
4 28
5 29
6 30
7 31
8 32
9 33
10 34
NULL
Run Code Online (Sandbox Code Playgroud)
但是,我想得到:
> apply(values, 1, function(x) cat(x[1], x[2], fill=TRUE))
1 25
2 26
3 27
4 28
5 29
6 30
7 31
8 32
9 33
10 34
Run Code Online (Sandbox Code Playgroud)
那么,如何删除最终的NULL?
Spa*_*man 11
NULL是R解释器打印您键入的表达式的值 - apply.你可以将它分配到某个地方:
junk = apply(values, 1, function(x) cat(x[1], x[2], fill=TRUE))
Run Code Online (Sandbox Code Playgroud)
在这种情况下,它不会打印,或包装在'隐形':
invisible(apply(values, 1, function(x) cat(x[1], x[2], fill=TRUE)))
Run Code Online (Sandbox Code Playgroud)
请注意,只有当您以交互方式运行时才打印每一行,如果它在函数中,您将看不到它.