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

Die*_*ego 4 windows csv shell r pipe

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

我发现这个线程解释了如何在 Linux 上完成它:Quicker way to read single column of CSV file

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

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

但是,这似乎不适用于 Windows。

当使用pipe("cut -f1 -d, Main.csv")连接被打开但它不返回任何东西。

为了在 Windows 上工作,我需要使用什么函数/语法。

是否可以通过使用 shell() 来完成此操作?

谢谢,

迭戈

G. *_*eck 5

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

# 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"))
Run Code Online (Sandbox Code Playgroud)

  • 您可以安装 Rtools,而无需将其放在您的路径上。在这种情况下,不太可能发生任何冲突。一旦以这种方式安装,请尝试:`DF &lt;- read.csv(pipe("\\Rtools\\bin\\cut -f1,2 -d, in.csv"))` (2认同)