Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.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-Windows中使用shell或管道读取csv文件的列_Windows_R_Shell_Csv_Pipe - Fatal编程技术网

在R-Windows中使用shell或管道读取csv文件的列

在R-Windows中使用shell或管道读取csv文件的列,windows,r,shell,csv,pipe,Windows,R,Shell,Csv,Pipe,我正在寻找一种使用shell()或管道将csv文件中的几列读取到R中的方法 我发现了一条解释如何在Linux上实现这一点的线索: 在Linux上,这可以添加what参数: a <-as.data.frame(scan(pipe("cut -f1,2 -d, Main.csv"), what=list("character","character"),sep= ","

我正在寻找一种使用shell()或管道将csv文件中的几列读取到R中的方法

我发现了一条解释如何在Linux上实现这一点的线索:

在Linux上,这可以添加what参数:

a <-as.data.frame(scan(pipe("cut -f1,2 -d, Main.csv"),
                       what=list("character","character"),sep= ","))

a确保
cut
在您的路径上-它在。这对我很有用:

# check that cut is availble
Sys.which("cut")

# create test data
Lines <- "a,b,c
1,2,3
4,5,6"
cat(Lines, file = "in.csv")

# read it
DF <- read.csv(pipe("cut -f1,2 -d, in.csv"))
#检查切割是否可用
系统哪个(“切割”)
#创建测试数据
行
>系统时间(a dim(a)
[1] 4706   46
>系统时间(b dim(b)
[1] 2726    2
>系统时间(d dim(d)
[1] 1715    2
>库(数据表)
data.table 1.9.2帮助类型:帮助(“data.table”)
警告信息:
关闭未使用的连接3(C:\Windows\system32\cmd.exe/C:/Rtools/bin/cut-f1,2-d,in.csv)
>系统时间(e dim(e)
[1] 4706    2

谢谢G。我想我没有在我的windows中安装RTools,因为它使用Cygwin,而且我以前在Ruby的active record库中遇到过问题。当我回到我的windows计算机时,我必须再次检查一下,但如果这对您有效,可能就是问题所在。再次感谢。您可以安装RTools,而无需将其放在计算机上路径。在这种情况下,不太可能会有任何冲突。以这种方式安装后,请尝试:
DF Nice G。它工作正常。下面您可以看到我的测试结果。出于我还不明白的原因,大多数实现的行数都不准确。只有fread()比默认的read.csv()准确且速度更快。你知道原因吗,或者我可以如何改进这种行为?尝试减少示例数据,直到你得到它,只需要几行输入就可以生成非预期的行数。谢谢你,G。不确定我是否要探讨这个问题,但如果我这样做,我肯定会发回。谢谢,againIt是一个更干净的库(rbenchmark)。然后您可以一次显示所有函数。例如:基准测试(f1(x)、f2(x)、f3(x)、f4(x),列=c(“测试”、“复制”、“已用”、“相对”),order=“相对”,relative=“已用”,复制=10)。(在这种情况下,用fread(“c:/Rtools/bin/cut-f1,2-d,In.csv”)替换f1(x),等等。总之,data.table赢了。+1谢谢你,路易斯。我不知道那个库!
> system.time(a <- read.csv("in.csv"))
   user  system elapsed 
   1.24    0.04    1.26 
> dim(a)
[1] 4706   46
> system.time(b <-read.csv(pipe("C:/Rtools/bin/cut -f1,2 -d, in.csv")))
   user  system elapsed 
   0.22    1.27    2.37 
Warning message:
In scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :
  EOF within quoted string
> dim(b)
[1] 2726    2
> system.time(d <-as.data.frame(scan(pipe("C:/Rtools/bin/cut -f1,2 -d, in.csv"),
+                        what=list("character","character"),sep= ",")))
Read 1715 records
   user  system elapsed 
   0.31    1.19    2.47 
Warning message:
In scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :
  EOF within quoted string
> dim(d)
[1] 1715    2
> library(data.table)
data.table 1.9.2  For help type: help("data.table")
Warning message:
closing unused connection 3 (C:\Windows\system32\cmd.exe /c C:/Rtools/bin/cut -f1,2 -d, in.csv) 
> system.time(e <-fread("C:/Rtools/bin/cut -f1,2 -d, in.csv"))
   user  system elapsed 
   0.02    0.01    0.80 
> dim(e)
[1] 4706    2