An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120918/953d6a8a/attachment.pl>
extracting column and regular interval in R
10 messages · R. Michael Weylandt, Eliza Botto, David Winsemius +2 more
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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120918/adfac1cc/attachment.pl>
On 18-09-2012, at 16:55, eliza botto 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.
Indexing with
c(1,seq_len((length(x)-1)/M)*M+1)
is also an option.
It appears to be faster than Michael's method.
N <- 444
a <- 1:N
f1 <- function(x,M) {
x[c(TRUE,rep(FALSE,M-1))]
}
f2 <- function(x,M) {
x[c(1,seq_len((length(x)-1)/M)*M+1)]
}
M <- 37
M
z1 <- f1(a,M)
z2 <- f2(a,M)
identical(z1,z2)
[1] TRUE Nrep <- 100000 library(rbenchmark) benchmark(f1(a,M), f2(a,M), replications=Nrep)
benchmark(f1(a,M), f2(a,M), replications=Nrep)
test replications elapsed relative user.self sys.self user.child 1 f1(a, M) 100000 1.395 1.437 1.367 0.027 0 2 f2(a, M) 100000 0.971 1.000 0.959 0.011 0 For N <- 4440 the timings are
benchmark(f1(a,M), f2(a,M), replications=Nrep)
test replications elapsed relative user.self sys.self user.child 1 f1(a, M) 100000 4.636 2.483 4.569 0.065 0 2 f2(a, M) 100000 1.867 1.000 1.714 0.153 0 Berend
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120918/3a013721/attachment.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120918/08fac199/attachment.pl>
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
On 18-09-2012, at 19:21, 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..
When you use my method you could do the following
f3 <- function(x,M,start=1) {
x[c(start,seq_len((length(x)-1)/M)*M+start)]
}
M <- 37
A <- 1:(3*M)
f3(A,M)
f3(A,M,start=1)
f3(A,M,start=2)
When start is too large you may get NA's in the result.
You may have to get rid of them.
Berend
Try: x <- 1:444 start<-2
x[c(rep(FALSE,start-1),TRUE, rep(FALSE, 36-start+1))]
# [1] 2 39 76 113 150 187 224 261 298 335 372 409
start<-30
x[c(rep(FALSE,start-1),TRUE, rep(FALSE, 36-start+1))]
# [1] 30 67 104 141 178 215 252 289 326 363 400 437
start<-37
x[c(rep(FALSE,start-1),TRUE, rep(FALSE, 36-start+1))]
# [1] 37 74 111 148 185 222 259 296 333 370 407 444
Clint Bowman INTERNET: clint at ecy.wa.gov
Air Quality Modeler INTERNET: clint at math.utah.edu
Department of Ecology VOICE: (360) 407-6815
PO Box 47600 FAX: (360) 407-7534
Olympia, WA 98504-7600
USPS: PO Box 47600, Olympia, WA 98504-7600
Parcels: 300 Desmond Drive, Lacey, WA 98503-1274
On Tue, 18 Sep 2012, 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.. 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.
On 18-09-2012, at 19:21, 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..
I have defined 2 more functions for David's and Clint's solution and my solution.
f3 <- function(x,M,start=1) {
x[c(rep(FALSE,start-1),TRUE,rep(FALSE,M-start))]
}
f4 <- function(x,M,start=1) {
x[c(start,seq_len((length(x)-1)/M)*M+start)]
}
z3 <- f3(a,M,start=1)
z4 <- f4(a,M,start=1)
identical(z3,z1)
identical(z3,z2)
z5 <- f3(a,M,start=4)
z6 <- f4(a,M,start=4)
identical(z5,z6)
With
N <- 444
a <- 1:N
M <- 37
I get the following timing results.
benchmark(f3(a,M,start=4), f4(a,M,start=4), replications=Nrep)
test replications elapsed relative user.self sys.self 1 f3(a, M, start = 4) 100000 1.621 1.562 1.606 0.014 2 f4(a, M, start = 4) 100000 1.038 1.000 1.035 0.002 Compared to the timings for start=1 (previous post) the solution with rep becomes slightly slower. Berend