[R] Fetching a range of columns

Marc Schwartz marc_schwartz at comcast.net
Tue Sep 16 04:39:59 CEST 2008


on 09/15/2008 09:05 PM David Winsemius wrote:
> 
> On Sep 15, 2008, at 8:15 PM, jim holtman wrote:
> 
>> your colClasses should look like this:
>>
>> colClasses=c(rep("NULL",49), "numeric")
>>
>> On Mon, Sep 15, 2008 at 6:27 PM, David Winsemius
>> <dwinsemius at comcast.net> wrote:
>>>
>>>
>>>> tstdta <- read.table(file.choose(), colClasses =
>>>> append(rep("NULL",48),c("numeric","numeric"),after=45),header=FALSE,
>>>> skip=1)
>>>> tstdta
>>> V46 V47
>>> 1 177 181
>>> 2 178 182
>>> 3 179 183
>>> 4 180 184
> 
> I suppose that works as well. I thought the use of append was acceptable
> as well. If one knew there were 2500
> columns, and the desired data were in columns 100-149 then
> 
> colClasses = append(rep("NULL",2350), rep("numeric",150), after = 99)
> 
> ... seems fairly clean.

I may have missed this somewhere in the thread, but given large
dataframes with many columns, if one knows the sequence of column names
that are desired, as opposed to column index numbers, one can use ':'
with ranges of column names in the subset() function.

Using a modification of the example that David posted previously:

DF <- as.data.frame(matrix(1:200, nrow = 4))

> subset(DF, select = V37:V42)
  V37 V38 V39 V40 V41 V42
1 145 149 153 157 161 165
2 146 150 154 158 162 166
3 147 151 155 159 163 167
4 148 152 156 160 164 168


> subset(DF, select = c(V3:V7, V11, V37:V40))
  V3 V4 V5 V6 V7 V11 V37 V38 V39 V40
1  9 13 17 21 25  41 145 149 153 157
2 10 14 18 22 26  42 146 150 154 158
3 11 15 19 23 27  43 147 151 155 159
4 12 16 20 24 28  44 148 152 156 160

This of course is for the post-processing of a data frame that is
already in R.

HTH,

Marc Schwartz



More information about the R-help mailing list