[R] extracting column at regular intervals starting from different columns

David Winsemius dwinsemius at comcast.net
Tue Sep 18 19:40:04 CEST 2012


On Sep 18, 2012, at 10:21 AM, eliza botto wrote:

> 
> Dear useRs,
> i had a matrix with 31 rows and 444 columns and i wanted to extract every 37th column of that matrix starting from 1. more precisely i wanted to select columns 1, 38,75, 112 and so on. then doing the same by starting from column number 2(2,39,76,113.......).
> i was advised to use 
>> x[c(TRUE, rep(FALSE, 36)),]
> i works if it is start from first column but as i am very new to R i wanted to know what to do to make it work if i want to start column selection from column 2 and then from column 3 and so on.
> sorry for bothering you once again..

This will start at 4 and then be recycled every succeeding 37th item:

offset1= 3; offset2 = 37
c( rep(FALSE, 3), TRUE, rep(FALSE, offset2-offset1-1) )

Observe:
>  (1:100)[ c( rep(FALSE, 3), TRUE, rep(FALSE, offset2-offset1-1) ) ]
[1]  4 41 78

> regards
> eliza
> 
> 
>> From: eliza_botto at hotmail.com
>> To: michael.weylandt at gmail.com
>> Date: Tue, 18 Sep 2012 15:06:18 +0000
>> CC: r-help at r-project.org
>> Subject: Re: [R] extracting column and regular interval in R
>> 
>> 
>> thnkyou very much micheal.
>> i worked!!!
>> regards
>> eliza
>> 
>> 
>>> From: michael.weylandt at gmail.com
>>> Date: Tue, 18 Sep 2012 15:58:31 +0100
>>> Subject: Re: [R] extracting column and regular interval in R
>>> To: eliza_botto at hotmail.com
>>> CC: r-help at r-project.org
>>> 
>>> x[c(TRUE, rep(FALSE, 36)),]
>>> 
>>> and let recycling work its magic!
>>> 
>>> To concretize:
>>> 
>>> x <- 1:100
>>> 
>>> x[c(TRUE, rep(FALSE, 4))]
>>> 
>>> Cheers,
>>> Michael
>>> 
>>> On Tue, Sep 18, 2012 at 3:55 PM, eliza botto <eliza_botto at hotmail.com> wrote:
>>>> 
>>>> Dear R users,
>>>> i have a matrix with 31 rows and 444 columns and i want to extract every 37th column of that matrix starting from 1. more precisely i want to select columns 1, 38,75, 112 and so on. then doing the same by starting from column number 2(2,39,76,113.......).
>>>> i know that there is a manual way of doing it but i wanted to make it more quickly as i have fairly large data to dealth with.
>>>> 
>>>> thanks in advance
>>>> eliza
>>>> [[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.
>> 
>> [[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.
> 		 	   		  
> 	[[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.

David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list