Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read columns of a csv file using shell or pipe inside R - Windows

I'm looking for a way of reading only a few columns from a csv file into R using shell() or pipe.

I found this thread that explains how to accomplish that on Linux: Quicker way to read single column of CSV file

On Linux this works adding the what argument:

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

However this doesn't seem to work on Windows.

When using pipe("cut -f1 -d, Main.csv") the connection gets opened but it doesn't return anything.

What would be the functions/syntax I need to use in order to make this work on Windows.

Is is possible to accomplish this by using shell()?

Thanks,

Diego

like image 915
Diego Avatar asked Mar 19 '23 11:03

Diego


1 Answers

Make sure that cut is on your path - its in Rtools. This works for me:

# 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"))

Added

Rtools is now Rtools40 and cut is at C:\Rtools40\usr\bin\cut.exe .

like image 95
G. Grothendieck Avatar answered Apr 06 '23 07:04

G. Grothendieck