Re: [R] Behavior of seq with vector from
Hi, Perhaps you can try this, seq.weave - function(froms, by, length, ... ){ c( matrix(c(sapply(froms, seq, by=by, length = length/2, ...)), nrow=length(froms), byrow=T) ) } seq.weave(c(2, 3), by=3, length=8) seq.weave(c(2, 3, 4), by=2, length=8) HTH, baptiste On 21 May 2009, at 21:08, Rowe, Brian Lee Yung (Portfolio Analytics) wrote: Hello, I want to use seq with multiple from values and am getting unexpected (to me) behavior. I'm wondering if this behavior is intentional or not. seq(2, by=3, length.out=4) [1] 2 5 8 11 seq(3, by=3, length.out=4) [1] 3 6 9 12 Now if I want the combined sequence, I thought I could pass in c(2,3), and I get: seq(c(2,3), by=3, length.out=8) [1] 2 6 8 12 14 18 20 24 However, the result is not what I expected (i.e. what I wanted): [1] 2 3 5 6 8 9 11 12 It seems that this is a consequence of vector recycling during the summation in seq.default: if (missing(to)) from + (0L:(length.out - 1L)) * by To get the value I want, I am using the following code: sort(as.vector(apply(array(c(2,3)), 1, seq, by=3,length.out=4))) [1] 2 3 5 6 8 9 11 12 So two questions: 1. Is seq designed/intended to be used with a vector from argument, and is this the desired behavior? 2. If so, is there a cleaner way of implementing what I want? Thanks, Brian -- This message w/attachments (message) may be privileged...{{dropped:26}} __ R-help@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.
Re: [R] Behavior of seq with vector from
Rowe, Brian Lee Yung (Portfolio Analytics) wrote: To get the value I want, I am using the following code: sort(as.vector(apply(array(c(2,3)), 1, seq, by=3,length.out=4))) [1] 2 3 5 6 8 9 11 12 So two questions: 1. Is seq designed/intended to be used with a vector from argument, and is this the desired behavior? 2. If so, is there a cleaner way of implementing what I want? 1. Hmm, not really. NA. 2. I'd view it as an outer sum, stringed out to a single vector, hence: c(outer(c(2,3), seq(0,,3,4), +)) [1] 2 3 5 6 8 9 11 12 -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - (p.dalga...@biostat.ku.dk) FAX: (+45) 35327907 __ R-help@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.
Re: [R] Behavior of seq with vector from
So if I want to concatenate the output of multiple seq calls, there's no clear way to to do this? For background, I have a number of data.frames with the same structure in a list. I want to 'collapse' the list into a single data.frame but only keeping certain columns from each underlying data.frame. In my example below, I want to keep columns 2,3 in each underlying data.frame. I'm using do.call('cbind', my.list) and then using the statement below to extract only the columns I need (other details omitted for brevity). If there's a built-in or pre-built function to do this, I'm all eyes. Brian PS if this is unclear, flame away, and I'll post some code -Original Message- From: Peter Dalgaard [mailto:p.dalga...@biostat.ku.dk] Sent: Friday, May 22, 2009 6:20 AM To: Rowe, Brian Lee Yung (Portfolio Analytics) Cc: r-help@r-project.org Subject: Re: [R] Behavior of seq with vector from Rowe, Brian Lee Yung (Portfolio Analytics) wrote: To get the value I want, I am using the following code: sort(as.vector(apply(array(c(2,3)), 1, seq, by=3,length.out=4))) [1] 2 3 5 6 8 9 11 12 So two questions: 1. Is seq designed/intended to be used with a vector from argument, and is this the desired behavior? 2. If so, is there a cleaner way of implementing what I want? 1. Hmm, not really. NA. 2. I'd view it as an outer sum, stringed out to a single vector, hence: c(outer(c(2,3), seq(0,,3,4), +)) [1] 2 3 5 6 8 9 11 12 -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - (p.dalga...@biostat.ku.dk) FAX: (+45) 35327907 -- This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. References to Merrill Lynch are references to any company in the Merrill Lynch Co., Inc. group of companies, which are wholly-owned by Bank of America Corporation. Securities and Insurance Products: * Are Not FDIC Insured * Are Not Bank Guaranteed * May Lose Value * Are Not a Bank Deposit * Are Not a Condition to Any Banking Service or Activity * Are Not Insured by Any Federal Government Agency. Attachments that are part of this E-communication may have additional important disclosures and disclaimers, which you should read. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing. -- __ R-help@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.
Re: [R] Behavior of seq with vector from
Try it this way: # test list of data frames L - list(anscombe[1:4], anscombe[5:8], anscombe[1:4], anscombe[5:8]) # get columns 2 and 3 from each component; cbind those together do.call(cbind, lapply(L, [, 2:3)) On Fri, May 22, 2009 at 11:01 AM, Rowe, Brian Lee Yung (Portfolio Analytics) b_r...@ml.com wrote: So if I want to concatenate the output of multiple seq calls, there's no clear way to to do this? For background, I have a number of data.frames with the same structure in a list. I want to 'collapse' the list into a single data.frame but only keeping certain columns from each underlying data.frame. In my example below, I want to keep columns 2,3 in each underlying data.frame. I'm using do.call('cbind', my.list) and then using the statement below to extract only the columns I need (other details omitted for brevity). If there's a built-in or pre-built function to do this, I'm all eyes. Brian PS if this is unclear, flame away, and I'll post some code -Original Message- From: Peter Dalgaard [mailto:p.dalga...@biostat.ku.dk] Sent: Friday, May 22, 2009 6:20 AM To: Rowe, Brian Lee Yung (Portfolio Analytics) Cc: r-help@r-project.org Subject: Re: [R] Behavior of seq with vector from Rowe, Brian Lee Yung (Portfolio Analytics) wrote: To get the value I want, I am using the following code: sort(as.vector(apply(array(c(2,3)), 1, seq, by=3,length.out=4))) [1] 2 3 5 6 8 9 11 12 So two questions: 1. Is seq designed/intended to be used with a vector from argument, and is this the desired behavior? 2. If so, is there a cleaner way of implementing what I want? 1. Hmm, not really. NA. 2. I'd view it as an outer sum, stringed out to a single vector, hence: c(outer(c(2,3), seq(0,,3,4), +)) [1] 2 3 5 6 8 9 11 12 -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - (p.dalga...@biostat.ku.dk) FAX: (+45) 35327907 -- This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. References to Merrill Lynch are references to any company in the Merrill Lynch Co., Inc. group of companies, which are wholly-owned by Bank of America Corporation. Securities and Insurance Products: * Are Not FDIC Insured * Are Not Bank Guaranteed * May Lose Value * Are Not a Bank Deposit * Are Not a Condition to Any Banking Service or Activity * Are Not Insured by Any Federal Government Agency. Attachments that are part of this E-communication may have additional important disclosures and disclaimers, which you should read. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing. -- __ R-help@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. __ R-help@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.
Re: [R] Behavior of seq with vector from
##Is this what you mean ?? do.call(rbind,lapply(yourlist,[,,seq(from=1,by=3,length=4))) ## note the ,, that omits the row argument in the call to [ -- Bert Gunter Genentech Nonclinical Biostatistics -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Rowe, Brian Lee Yung (Portfolio Analytics) Sent: Friday, May 22, 2009 8:02 AM To: Peter Dalgaard Cc: r-help@r-project.org Subject: Re: [R] Behavior of seq with vector from So if I want to concatenate the output of multiple seq calls, there's no clear way to to do this? For background, I have a number of data.frames with the same structure in a list. I want to 'collapse' the list into a single data.frame but only keeping certain columns from each underlying data.frame. In my example below, I want to keep columns 2,3 in each underlying data.frame. I'm using do.call('cbind', my.list) and then using the statement below to extract only the columns I need (other details omitted for brevity). If there's a built-in or pre-built function to do this, I'm all eyes. Brian PS if this is unclear, flame away, and I'll post some code -Original Message- From: Peter Dalgaard [mailto:p.dalga...@biostat.ku.dk] Sent: Friday, May 22, 2009 6:20 AM To: Rowe, Brian Lee Yung (Portfolio Analytics) Cc: r-help@r-project.org Subject: Re: [R] Behavior of seq with vector from Rowe, Brian Lee Yung (Portfolio Analytics) wrote: To get the value I want, I am using the following code: sort(as.vector(apply(array(c(2,3)), 1, seq, by=3,length.out=4))) [1] 2 3 5 6 8 9 11 12 So two questions: 1. Is seq designed/intended to be used with a vector from argument, and is this the desired behavior? 2. If so, is there a cleaner way of implementing what I want? 1. Hmm, not really. NA. 2. I'd view it as an outer sum, stringed out to a single vector, hence: c(outer(c(2,3), seq(0,,3,4), +)) [1] 2 3 5 6 8 9 11 12 -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - (p.dalga...@biostat.ku.dk) FAX: (+45) 35327907 -- This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. References to Merrill Lynch are references to any company in the Merrill Lynch Co., Inc. group of companies, which are wholly-owned by Bank of America Corporation. Securities and Insurance Products: * Are Not FDIC Insured * Are Not Bank Guaranteed * May Lose Value * Are Not a Bank Deposit * Are Not a Condition to Any Banking Service or Activity * Are Not Insured by Any Federal Government Agency. Attachments that are part of this E-communication may have additional important disclosures and disclaimers, which you should read. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing. -- __ R-help@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. __ R-help@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.
Re: [R] Behavior of seq with vector from
Yet another way of doing it: x - c(2,3) x + rep(seq(0, by=3, length=4), each=length(x)) [1] 2 3 5 6 8 9 11 12 On Fri, May 22, 2009 at 11:01 AM, Rowe, Brian Lee Yung (Portfolio Analytics) b_r...@ml.com wrote: So if I want to concatenate the output of multiple seq calls, there's no clear way to to do this? For background, I have a number of data.frames with the same structure in a list. I want to 'collapse' the list into a single data.frame but only keeping certain columns from each underlying data.frame. In my example below, I want to keep columns 2,3 in each underlying data.frame. I'm using do.call('cbind', my.list) and then using the statement below to extract only the columns I need (other details omitted for brevity). If there's a built-in or pre-built function to do this, I'm all eyes. Brian PS if this is unclear, flame away, and I'll post some code -Original Message- From: Peter Dalgaard [mailto:p.dalga...@biostat.ku.dk] Sent: Friday, May 22, 2009 6:20 AM To: Rowe, Brian Lee Yung (Portfolio Analytics) Cc: r-help@r-project.org Subject: Re: [R] Behavior of seq with vector from Rowe, Brian Lee Yung (Portfolio Analytics) wrote: To get the value I want, I am using the following code: sort(as.vector(apply(array(c(2,3)), 1, seq, by=3,length.out=4))) [1] 2 3 5 6 8 9 11 12 So two questions: 1. Is seq designed/intended to be used with a vector from argument, and is this the desired behavior? 2. If so, is there a cleaner way of implementing what I want? 1. Hmm, not really. NA. 2. I'd view it as an outer sum, stringed out to a single vector, hence: c(outer(c(2,3), seq(0,,3,4), +)) [1] 2 3 5 6 8 9 11 12 -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - (p.dalga...@biostat.ku.dk) FAX: (+45) 35327907 -- This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. References to Merrill Lynch are references to any company in the Merrill Lynch Co., Inc. group of companies, which are wholly-owned by Bank of America Corporation. Securities and Insurance Products: * Are Not FDIC Insured * Are Not Bank Guaranteed * May Lose Value * Are Not a Bank Deposit * Are Not a Condition to Any Banking Service or Activity * Are Not Insured by Any Federal Government Agency. Attachments that are part of this E-communication may have additional important disclosures and disclaimers, which you should read. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing. -- __ R-help@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.htmlhttp://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? [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Behavior of seq with vector from
Brilliant! Thanks, Brian -Original Message- From: Gabor Grothendieck [mailto:ggrothendi...@gmail.com] Sent: Friday, May 22, 2009 11:20 AM To: Rowe, Brian Lee Yung (Portfolio Analytics) Cc: Peter Dalgaard; r-help@r-project.org Subject: Re: [R] Behavior of seq with vector from Try it this way: # test list of data frames L - list(anscombe[1:4], anscombe[5:8], anscombe[1:4], anscombe[5:8]) # get columns 2 and 3 from each component; cbind those together do.call(cbind, lapply(L, [, 2:3)) On Fri, May 22, 2009 at 11:01 AM, Rowe, Brian Lee Yung (Portfolio Analytics) b_r...@ml.com wrote: So if I want to concatenate the output of multiple seq calls, there's no clear way to to do this? For background, I have a number of data.frames with the same structure in a list. I want to 'collapse' the list into a single data.frame but only keeping certain columns from each underlying data.frame. In my example below, I want to keep columns 2,3 in each underlying data.frame. I'm using do.call('cbind', my.list) and then using the statement below to extract only the columns I need (other details omitted for brevity). If there's a built-in or pre-built function to do this, I'm all eyes. Brian PS if this is unclear, flame away, and I'll post some code -Original Message- From: Peter Dalgaard [mailto:p.dalga...@biostat.ku.dk] Sent: Friday, May 22, 2009 6:20 AM To: Rowe, Brian Lee Yung (Portfolio Analytics) Cc: r-help@r-project.org Subject: Re: [R] Behavior of seq with vector from Rowe, Brian Lee Yung (Portfolio Analytics) wrote: To get the value I want, I am using the following code: sort(as.vector(apply(array(c(2,3)), 1, seq, by=3,length.out=4))) [1] 2 3 5 6 8 9 11 12 So two questions: 1. Is seq designed/intended to be used with a vector from argument, and is this the desired behavior? 2. If so, is there a cleaner way of implementing what I want? 1. Hmm, not really. NA. 2. I'd view it as an outer sum, stringed out to a single vector, hence: c(outer(c(2,3), seq(0,,3,4), +)) [1] 2 3 5 6 8 9 11 12 -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - (p.dalga...@biostat.ku.dk) FAX: (+45) 35327907 -- This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. References to Merrill Lynch are references to any company in the Merrill Lynch Co., Inc. group of companies, which are wholly-owned by Bank of America Corporation. Securities and Insurance Products: * Are Not FDIC Insured * Are Not Bank Guaranteed * May Lose Value * Are Not a Bank Deposit * Are Not a Condition to Any Banking Service or Activity * Are Not Insured by Any Federal Government Agency. Attachments that are part of this E-communication may have additional important disclosures and disclaimers, which you should read. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing. -- __ R-help@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. __ R-help@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.
[R] Behavior of seq with vector from
Hello, I want to use seq with multiple from values and am getting unexpected (to me) behavior. I'm wondering if this behavior is intentional or not. seq(2, by=3, length.out=4) [1] 2 5 8 11 seq(3, by=3, length.out=4) [1] 3 6 9 12 Now if I want the combined sequence, I thought I could pass in c(2,3), and I get: seq(c(2,3), by=3, length.out=8) [1] 2 6 8 12 14 18 20 24 However, the result is not what I expected (i.e. what I wanted): [1] 2 3 5 6 8 9 11 12 It seems that this is a consequence of vector recycling during the summation in seq.default: if (missing(to)) from + (0L:(length.out - 1L)) * by To get the value I want, I am using the following code: sort(as.vector(apply(array(c(2,3)), 1, seq, by=3,length.out=4))) [1] 2 3 5 6 8 9 11 12 So two questions: 1. Is seq designed/intended to be used with a vector from argument, and is this the desired behavior? 2. If so, is there a cleaner way of implementing what I want? Thanks, Brian -- This message w/attachments (message) may be privileged, ...{{dropped:27}} __ R-help@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.
Re: [R] Behavior of seq with vector from
Rowe, Brian Lee Yung (Portfolio Analytics) wrote: Hello, I want to use seq with multiple from values and am getting unexpected (to me) behavior. I'm wondering if this behavior is intentional or not. seq(2, by=3, length.out=4) [1] 2 5 8 11 seq(3, by=3, length.out=4) [1] 3 6 9 12 Now if I want the combined sequence, I thought I could pass in c(2,3), and I get: seq(c(2,3), by=3, length.out=8) [1] 2 6 8 12 14 18 20 24 However, the result is not what I expected (i.e. what I wanted): [1] 2 3 5 6 8 9 11 12 It seems that this is a consequence of vector recycling during the summation in seq.default: if (missing(to)) from + (0L:(length.out - 1L)) * by To get the value I want, I am using the following code: sort(as.vector(apply(array(c(2,3)), 1, seq, by=3,length.out=4))) [1] 2 3 5 6 8 9 11 12 So two questions: 1. Is seq designed/intended to be used with a vector from argument, and is this the desired behavior? 2. If so, is there a cleaner way of implementing what I want? Thanks, Brian Don't know if this is cleaner or not. It may be a little bit too tricky. c(outer(2:3,seq(0,by=3,length.out=4),+)) Can compress your example slightly: c(t(apply(matrix(2:3), 1, seq, by=3,length.out=4))) -- View this message in context: http://www.nabble.com/Behavior-of-seq-with-vector-from-tp23658790p23663549.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.