[R] How to extract range of colums in a data frame

Marc Schwartz marc_schwartz at comcast.net
Mon Jan 5 03:58:58 CET 2009


Or this:

new.DF <- subset(dat, select = V3:V9)

> str(new.DF)
'data.frame':	16 obs. of  7 variables:
 $ V3: num  9 18 15 15 21 21 12 15 21 19.5 ...
 $ V4: int  18 8 8 6 21 21 21 9 21 18 ...
 $ V5: num  12 12 12 18 21 ...
 $ V6: num  18 18 12 6 21 21 21 18 21 19.5 ...
 $ V7: num  15 15 18 18 21 21 21 15 21 16.5 ...
 $ V8: num  12 12 12 15 21 21 21 15 21 19.5 ...
 $ V9: num  6 18 12 9 21 21 21 6 21 18 ...


See ?subset for the above and ?"[.data.frame" for additional information
on subsetting data frames, which is also covered in An Introduction to R.

HTH,

Marc Schwartz


on 01/04/2009 08:42 PM Jorge Ivan Velez wrote:
> Dear Gundala,
> Try this:
> 
>> ex<-paste("V",3:9,sep="")
>> new.dat<-dat[,ex]
>> new.dat
> 
> HTH,
> 
> Jorge
> 
> 
> 
> On Sun, Jan 4, 2009 at 9:36 PM, Gundala Viswanath <gundalav at gmail.com>wrote:
> 
>> Dear all,
>>
>> I have the following data frame:
>>
>>> dat
>>   V1                                 V2   V3 V4    V5   V6   V7   V8   V9
>> 1   1 AAAACACCCACCCCCCCCCCCCCCCCCCCCCCCC  9.0 18 12.00 18.0 15.0 12.0  6.0
>> 2   1 ACGATACGGCGACCACCGAGATCTACACTCTTCC 18.0  8 12.00 18.0 15.0 12.0 18.0
>> 3   1 ACTACTGCTCCCCCCCCACTCCCCCCCCCCCCCC 15.0  8 12.00 12.0 18.0 12.0 12.0
>> 4   1 ACTTATACGGCGACCACCGAGATCTACACTCTTT 15.0  6 18.00  6.0 18.0 15.0  9.0
>> 5   1 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC 21.0 21 21.00 21.0 21.0 21.0 21.0
>> 6   1 CTACACTCTTTCCCTACACGCCGCTCTTCCGATC 21.0 21 21.00 21.0 21.0 21.0 21.0
>> 7   1 TACACCGCCCCCCCCCATCTCCACACTCTCCCCC 12.0 21 12.00 21.0 21.0 21.0 21.0
>> 8   1 TGATACGCCTACCACCGCCCTCTACACTCTCTCC 15.0  9 18.00 18.0 15.0 15.0  6.0
>> 9   1 TGATACGGCGACCACCGAGATCTACACTCTCTCC 21.0 21 21.00 21.0 21.0 21.0 21.0
>> 10  4 TGATACGGCGACCACCGAGATCTACACTCTTTCC 19.5 18 15.75 19.5 16.5 19.5 18.0
>> 11  1 TGATACGGCGACCACCGAGGATCTACACTCTTTC 21.0 21 21.00 21.0 21.0 21.0 21.0
>> 12  1 TGATACGGCGACCACCGAGGATCTCCACTCTCTC 21.0 21 21.00 21.0 21.0 21.0 21.0
>> 13  2 TGCTCCGGCGACCACCGAGATCTACACTCTTTCC 18.0  8 12.00 18.0 13.5 18.0 13.5
>> 14  1 TTATACGTCGACCACCGAGATCTACACTCTCTCC 18.0 18 18.00 18.0 18.0 18.0 15.0
>> 15  1 TTCTCCGGCGACCACCGAGATCTACACTCTTTCC 18.0  7  9.00 18.0 12.0 18.0 15.0
>> 16  1 TTCTCCGGCGACCACCGCGATCTACACTCTTTCC 18.0  7  9.00 18.0 12.0 18.0 15.0
>>
>>
>> My question is how can I extract the column V3 up to V9 into another
>> new data frame.
>>
>> I tried this but failed:
>> str <- paste("V", 3:9, sep="")
>> print(dat$str)
>>




More information about the R-help mailing list