当前位置:  开发笔记 > 开发工具 > 正文

何时以及为什么"打印"需要两次尝试打印"data.table"?

如何解决《何时以及为什么"打印"需要两次尝试打印"data.table"?》经验,为你挑选了0个好方法。

有时print需要两次尝试打印data.table:

> library(data.table)
> 
> rm(list=ls())
> 
> Tbl <- fread( input = "Nr; Value
+                        Nr 1;46.73
+                        Nr 2;49.02
+                        Nr 3;50.62
+                        Nr 4;49.80
+                        Nr 5;50.15",
+               sep    = ";",
+               header = TRUE,
+               colClasses = c("character","numeric") )
> print(Tbl)
     Nr Value
1: Nr 1 46.73
2: Nr 2 49.02
3: Nr 3 50.62
4: Nr 4 49.80
5: Nr 5 50.15
> Tbl <- Tbl[, Nr := as.numeric( gsub( "Nr ", "", Tbl$Nr ))]
> print(Tbl)
> print(Tbl)
   Nr Value
1:  1 46.73
2:  2 49.02
3:  3 50.62
4:  4 49.80
5:  5 50.15
> 

不是这样的data.frame:

> rm(list=ls())
> 
> DF <- read.table( text = "Nr; Value
+                           Nr 1;46.73
+                           Nr 2;49.02
+                           Nr 3;50.62
+                           Nr 4;49.80
+                           Nr 5;50.15",
+                   sep    = ";",
+                   header = TRUE,
+                   colClasses = c("character","numeric"))
> 
> DF$Nr <- as.numeric( gsub( "Nr ", "", DF$Nr ))
> print(DF)
  Nr Value
1  1 46.73
2  2 49.02
3  3 50.62
4  4 49.80
5  5 50.15
> 

如果代码包含在脚本文件中,data.table则立即打印:

> source(path_to_Script_1,echo=TRUE,prompt.echo="(script)   ",max.deparse.length=500)

(script)   library(data.table)

(script)   rm(list=ls())

(script)   Tbl <- fread( input = "Nr; Value
+                        Nr 1;46.73
+                        Nr 2;49.02
+                        Nr 3;50.62
+                        Nr 4;49.80
+                        Nr 5;50.15",
+               sep    = ";",
+               header = TRUE,
+               colClasses = c("character","numeric") )

(script)   Tbl <- Tbl[, Nr := as.numeric( gsub( "Nr ", "", Tbl$Nr ))]

(script)   print(Tbl)
   Nr Value
1:  1 46.73
2:  2 49.02
3:  3 50.62
4:  4 49.80
5:  5 50.15
> 

但是如果print(Tbl)从脚本文件中省略,则print在控制台上再次需要两次尝试:

> source(path_to_Script_2,echo=TRUE,prompt.echo="(script)   ",max.deparse.length=500)

(script)   library(data.table)

(script)   rm(list=ls())

(script)   Tbl <- fread( input = "Nr; Value
+                        Nr 1;46.73
+                        Nr 2;49.02
+                        Nr 3;50.62
+                        Nr 4;49.80
+                        Nr 5;50.15",
+               sep    = ";",
+               header = TRUE,
+               colClasses = c("character","numeric") )

(script)   Tbl <- Tbl[, Nr := as.numeric( gsub( "Nr ", "", Tbl$Nr ))]
> print(Tbl)
> print(Tbl)
   Nr Value
1:  1 46.73
2:  2 49.02
3:  3 50.62
4:  4 49.80
5:  5 50.15
> 

任何人都可以告诉我什么时候为什么print需要两次尝试?我正在使用R版本3.2.2:

> R.version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          3                           
minor          2.2                         
year           2015                        
month          08                          
day            14                          
svn rev        69053                       
language       R                           
version.string R version 3.2.2 (2015-08-14)
nickname       Fire Safety   

操作系统是Windows 7.

推荐阅读
手机用户2502852037
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有