[R] Fetching a range of columns

jim holtman jholtman at gmail.com
Sun Sep 14 20:08:10 CEST 2008


Have you tried:

data_filter <- data[842:2411]

Also if you have a lot of data to read, I would suggest that you use a
connection, and it all the data is numeric, possibly 'scan'.  If you
do use a connection, this would eliminate having to 'skip' each time
which could be time consuming on a large file.  Since it appears that
you are not writing out the column names in the output file, you could
bypass the header line on the file by readLine after the open.  So
something like this might work:

input <- file('yourfile','r')
invisible(readLines(input, n=1))  # skip the header
while (TRUE){  # read file
    x <- try(read.csv(input, n=320, header=FALSE), silent=TRUE)  # catch EOF
    if (inherits(x, 'try-error')) break
    write.csv(.......)
}



On Sun, Sep 14, 2008 at 12:22 PM, Jason Thibodeau <jbloudg20 at gmail.com> wrote:
> Hello,
>
> I realize that using: x[x > 3 & x < 5] I can fetch all elements between 3
> and 5. However I read in from a CSV file, and I would like to fetch all
> columns from within a range ( 842-2411). In teh past, I have done this to
> fetch just select few columns:
>
> data <- read.csv(filein, header=TRUE, nrows=320, skip=nskip)
>                        data_filter <- data[c(2,12,17)]
>                        write.table(data_filter, fileout, append = TRUE,
> sep= ",", row.names= FALSE, col.names = FALSE)
>                        nskip <- nskip+320
>
> This time, however, instead of grabbing columns 2, 12, 17, I woudl like all
> columns in the range of 842-2411. I can't seem to do this correctly. Could
> somebody please provide some insight? Thanks in advance.
>
> --
> Jason Thibodeau
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list