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() 来完成此操作?
谢谢,
迭戈
确保它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)