Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R格式NaN函数_R_Formatting_Format_Latex - Fatal编程技术网

R格式NaN函数

R格式NaN函数,r,formatting,format,latex,R,Formatting,Format,Latex,我试图找到一个format函数,它将抑制R中的NaN输出。我想传入一个double向量,让NaN值返回为空,而不是NaN。我正在尝试Latex表的格式输出。这应该很简单吧?有这样的功能吗 我现在得到的是: > x <- c(seq(1,2,0.2), NaN) > as.character(x) > [1] "1" "1.2" "1.4" "1.6" "1.8" "2" "NaN" 这就是我想要的: > x <- c(seq(1,2,0.2), NaN

我试图找到一个format函数,它将抑制R中的NaN输出。我想传入一个double向量,让NaN值返回为空,而不是NaN。我正在尝试Latex表的格式输出。这应该很简单吧?有这样的功能吗

我现在得到的是:

> x <- c(seq(1,2,0.2), NaN)
> as.character(x)
> [1] "1"   "1.2" "1.4" "1.6" "1.8" "2"  "NaN"
这就是我想要的:

> x <- c(seq(1,2,0.2), NaN)
> formatting.function(x)
> [1] "1"   "1.2" "1.4" "1.6" "1.8" "2"  ""
给你:

R> x <- c(seq(1,2,0.2), NaN)
R> zx <- as.character(x)
R> zx
[1] "1"   "1.2" "1.4" "1.6" "1.8" "2"   "NaN"
并使用它:

R> zy <- mattFun(x)
R> zy
[1] "1"   "1.2" "1.4" "1.6" "1.8" "2"   ""   
R> 
认真地说,您只是在寻找一个简单的模式替换,这是什么 正则表达式可以。gsub是提供该功能的几个函数之一。尝试阅读正则表达式。

只需将NaN替换为:

x <- c(seq(1,2,0.2), NaN)
fofu <- function(x){
   cx <- as.character(x)
   cx[cx=="NaN"] <- ""
   cx
   }
fofu(x)
#[1] "1"   "1.2" "1.4" "1.6" "1.8" "2"   ""
编辑 或者使用隐式转换使其变短:

fofu2 <- function(x)  "[<-"(x, is.nan(x), "")
# or ... replace(x, is.nan(x), "")
这比使用上面定义的microbenchmark和x的基于gsub的解决方案快大约两倍——当然,大多数时候最重要的是方便,而不是计算时间

fofu2 <- function(x)  "[<-"(x, is.nan(x), "")
# or ... replace(x, is.nan(x), "")