[R] Fetching a range of columns

David Winsemius dwinsemius at comcast.net
Sun Sep 14 22:24:23 CEST 2008


On Sep 14, 2008, at 4:01 PM, Jason Thibodeau wrote:

> TEST_filter("line50grab.csv","line50grab_filterout.csv")
> Error in `[.data.frame`(data_tmp, seq(842, 2411)) :
>  undefined columns selected
>

I am guessing that you wrapped some code into a function but you did  
not provide the function. You are not really following the posting  
guidelines here.


> I know my file has about 3000 columns.
>
> This happened when I used:
> data_tmp <- read.csv(filein, header=TRUE, nrows=10, skip=nskip)
>                        data_filter <- data_tmp[seq(842,2411)]
>                        write.table(data_filter, fileout, append =  
> TRUE,
> sep= ",", row.names= FALSE, col.names = FALSE)
>
> Also using data_tmp[842:2411] did not yield any output being written  
> to my
> file.

Not a big surprise. Appears the error preceded the write.table call.

> I have another slightly unrelated problem, but I'll propose that  
> after this
> one can be solved.

If the problem is not with the syntax or semantics of TEST_filter as I  
suspect, then perhaps you should examine the input file from R's  
perspective with:

?count.fields

Hard to tell without the actual code and sample data.

-- 
David Winsemius

>
>
> Thanks a lot.
>
> On Sun, Sep 14, 2008 at 2:14 PM, Jason Thibodeau  
> <jbloudg20 at gmail.com>wrote:
>
>> Jim, this is a GREAT help. I was trying something similar before,  
>> but I was
>> unable to detect EOF. Thanks for the help!
>>
>> Also, David, your suggestion worked perfectly.
>>
>> Thanks for all the help, everyone!
>>
>>
>> On Sun, Sep 14, 2008 at 2:08 PM, jim holtman <jholtman at gmail.com>  
>> wrote:
>>
>>> 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?
>>>
>>
>>
>>
>> --
>> Jason Thibodeau
>>
>
>
>
> -- 
> 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.



More information about the R-help mailing list