[R] Sequence generation in a table

2010-12-09 Thread Vincy Pyne




Dear R helpers

I have following input

f = c(257, 520, 110). I need to generate a decreasing sequence (decreasing by 
100) which will give me an input (in a tabular form) like

257, 157, 57
520, 420, 320, 220, 120, 20
110, 10


I tried the following R code


f = c(257, 520, 110)
yy = matrix(data = NA, nrow = 3, ncol = 6)

for (i in 1:3)
 {
 value = NULL

 for (j in 1 : 6)
  {
  value = c(ans, seq(f[i], 1, by = -100))
  }
    yy[i,] = ans[i,j]
    }

I get following message

Error in ans[i, j] : incorrect number of dimensions. Also, I understand above 
logic will generate a result in (3 by 6) matrix format, while I need to 
generate only 3 numbers pertaining to first no. i.e. 257, 6 nos. beginning from 
520, and only 2 numbers beginning from 110. I also tried tapply etc.

Please guide

Vincy





[[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] Sequence generation in a table

2010-12-09 Thread Jan van der Laan

Vincy,

I suppose the following does what you want. yy is now a list which 
allows for differing lengths of the vectors.


 yy - lapply(c(257, 520, 110), seq, to=0, by=-100)
 yy[[1]]
[1] 257 157  57
 yy[[2]]
[1] 520 420 320 220 120  20


Regards,
Jan


On 9-12-2010 11:40, Vincy Pyne wrote:

c(257, 520, 110)


__
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] Sequence generation in a table

2010-12-09 Thread Vincy Pyne
Dear Sir,

Sorry to bother you again. Sir, the R code provided by you gives me following 
output.

 yy - lapply(c(257, 520, 110), seq, to=0, by=-100)
 yy
[[1]]
[1] 257 157  57

[[2]]
[1] 520 420 320 220 120  20

[[3]]
[1] 110  10

The biggest constraint for me is here as an example I have taken only three 
cases i.e. c(257, 520, 110), however in reality I will be dealing with no of 
cases and that number is unknown. But your code will certainly generate me the 
required numbers. In above case for doing further calculations, I can define 
say 

yy1 = as.numeric(yy[[1]])
yy2 = as.numeric(yy[2]])
yy3 = as.numeric(yy[[3]])

But when the number of cases are unknown, perhaps this is not the practical way 
of me defining individually. So is there any way that I can have all the 
sequence numbers generated can be accommodated in a single dataframe. I 
sincerely apologize for disturbing you Sir and hope I am able to put up my 
problem in a proper manner.

Regards

Vincy Pyne


--- On Thu, 12/9/10, Jan van der Laan rh...@eoos.dds.nl wrote:

From: Jan van der Laan rh...@eoos.dds.nl
Subject: Re: [R] Sequence generation in a table
To: r-help@r-project.org, vincy_p...@yahoo.ca
Received: Thursday, December 9, 2010, 10:57 AM

Vincy,

I suppose the following does what you want. yy is now a list which allows for 
differing lengths of the vectors.

 yy - lapply(c(257, 520, 110), seq, to=0, by=-100)
 yy[[1]]
[1] 257 157  57
 yy[[2]]
[1] 520 420 320 220 120  20


Regards,
Jan


On 9-12-2010 11:40, Vincy Pyne wrote:
 c(257, 520, 110)



[[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] Sequence generation in a table

2010-12-09 Thread Liviu Andronic
On Thu, Dec 9, 2010 at 12:41 PM, Vincy Pyne vincy_p...@yahoo.ca wrote:
 The biggest constraint for me is here as an example I have taken only three 
 cases i.e. c(257, 520, 110), however in reality I will be dealing with no of 
 cases and that number is unknown. But your code will certainly generate me 
 the required numbers. In above case for doing further calculations, I can 
 define say

 yy1 = as.numeric(yy[[1]])
 yy2 = as.numeric(yy[2]])
 yy3 = as.numeric(yy[[3]])

Why do you want to do that? For example,
 mean(yy[[1]])
[1] 157
 mean(yy1)
[1] 157

Liviu

__
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] Sequence generation in a table

2010-12-09 Thread Petr PIKAL
Hi

r-help-boun...@r-project.org napsal dne 09.12.2010 12:41:47:

 Dear Sir,
 
 Sorry to bother you again. Sir, the R code provided by you gives me 
following output.
 
  yy - lapply(c(257, 520, 110), seq, to=0, by=-100)
  yy
 [[1]]
 [1] 257 157  57
 
 [[2]]
 [1] 520 420 320 220 120  20
 
 [[3]]
 [1] 110  10
 
 The biggest constraint for me is here as an example I have taken only 
three 
 cases i.e. c(257, 520, 110), however in reality I will be dealing with 
no of 
 cases and that number is unknown. But your code will certainly generate 
me the
 required numbers. In above case for doing further calculations, I can 
define say 
 
 yy1 = as.numeric(yy[[1]])
 yy2 = as.numeric(yy[2]])
 yy3 = as.numeric(yy[[3]])

Why? Those values are already numeric.

lapply(yy, is.numeric)

[[1]]
[1] TRUE

[[2]]
[1] TRUE

[[3]]
[1] TRUE

and you can use the same construction to perform almost any operation on 
list.

lapply(yy, max)
lapply(yy, mean)
lapply(yy, sd)
lapply(yy, t.test)

Regards
Petr


 
 But when the number of cases are unknown, perhaps this is not the 
practical 
 way of me defining individually. So is there any way that I can have all 
the 
 sequence numbers generated can be accommodated in a single dataframe. I 
 sincerely apologize for disturbing you Sir and hope I am able to put up 
my 
 problem in a proper manner.
 
 Regards
 
 Vincy Pyne
 
 
 --- On Thu, 12/9/10, Jan van der Laan rh...@eoos.dds.nl wrote:
 
 From: Jan van der Laan rh...@eoos.dds.nl
 Subject: Re: [R] Sequence generation in a table
 To: r-help@r-project.org, vincy_p...@yahoo.ca
 Received: Thursday, December 9, 2010, 10:57 AM
 
 Vincy,
 
 I suppose the following does what you want. yy is now a list which 
allows for 
 differing lengths of the vectors.
 
  yy - lapply(c(257, 520, 110), seq, to=0, by=-100)
  yy[[1]]
 [1] 257 157  57
  yy[[2]]
 [1] 520 420 320 220 120  20
 
 
 Regards,
 Jan
 
 
 On 9-12-2010 11:40, Vincy Pyne wrote:
  c(257, 520, 110)
 
 
 
[[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.

__
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] Sequence generation in a table

2010-12-09 Thread Vincy Pyne
Dear Sirs,

I understand these already are numeric values. Sir, Basically I am working on 
Value at Risk for the Bond portfolio using the historical simulation and for 
this I need to find out Marked to Market (MTM) value suing the Present Value of 
the coupon payments for each Bonds (here as an example I have taken only 3). 

What I have done so far is for a given bond I have found no of days left for 
maturity. E.g. in 1st case there are 257 days left for maturity. The bond pays 
coupon twice a year and thus on 257th day the bond will mature and I will be 
getting the Principal and final coupon payment. Since teh bond is paying the 
coupons every 6 months, going backward from 257 th day, my earlier coupon 
payment falls on (257 - 180) = 77 days. (However, in above example, I have just 
taken 100 just for example purpose)

Thus, assuming 100 days, my coupons will be paid on 257, 157,  57days. I need 
to convert these days in terms of years and so when I try to divide yy defined 
as 

yy - lapply(c(257, 520,
 110), seq, to=0, by=-100)

yy/360, I get following error.

Error in yy/360 : non-numeric argument to binary operator

On the other hand, 

yy[[1]]/365  fetches me 

[1] 0.7138889 0.436 0.158


Thus, I am trying to obtain the result yy - lapply(c(257, 520,
 110), seq, to=0, by=-100) in such a form, so taht I should be able to further 
analysis. What I was trying to say is since here I am taking only three bonds, 
so I can do it individually, however if there are number of bonds (say 1000) in 
the portfolio, my method of converting the days individually is not practical.

I am extremely sorry for the inconvenience caused. I tried to keep my problem 
short in oder not to consume your valuable time.

Regards

Vince Pyne



--- On Thu, 12/9/10, Petr PIKAL petr.pi...@precheza.cz wrote:

From: Petr PIKAL petr.pi...@precheza.cz
Subject: Re: [R] Sequence generation in a table
To: Vincy Pyne vincy_p...@yahoo.ca
Cc: r-help@r-project.org
Received: Thursday, December 9, 2010, 12:03 PM

Hi

r-help-boun...@r-project.org napsal dne 09.12.2010 12:41:47:

 Dear Sir,
 
 Sorry to bother you again. Sir, the R code provided by you gives me 
following output.
 
  yy - lapply(c(257, 520,
 110), seq, to=0, by=-100)
  yy
 [[1]]
 [1] 257 157  57
 
 [[2]]
 [1] 520 420 320 220 120  20
 
 [[3]]
 [1] 110  10
 
 The biggest constraint for me is here as an example I have taken only 
three 
 cases i.e. c(257, 520, 110), however in reality I will be dealing with 
no of 
 cases and that number is unknown. But your code will certainly generate 
me the
 required numbers. In above case for doing further calculations, I can 
define say 
 
 yy1 = as.numeric(yy[[1]])
 yy2 = as.numeric(yy[2]])
 yy3 = as.numeric(yy[[3]])

Why? Those values are already numeric.

lapply(yy, is.numeric)

[[1]]
[1] TRUE

[[2]]
[1] TRUE

[[3]]
[1] TRUE

and you can use the same construction to perform almost any operation on 
list.

lapply(yy, max)
lapply(yy,
 mean)
lapply(yy, sd)
lapply(yy, t.test)

Regards
Petr


 
 But when the number of cases are unknown, perhaps this is not the 
practical 
 way of me defining individually. So is there any way that I can have all 
the 
 sequence numbers generated can be accommodated in a single dataframe. I 
 sincerely apologize for disturbing you Sir and hope I am able to put up 
my 
 problem in a proper manner.
 
 Regards
 
 Vincy Pyne
 
 
 --- On Thu, 12/9/10, Jan van der Laan rh...@eoos.dds.nl wrote:
 
 From: Jan van der Laan rh...@eoos.dds.nl
 Subject: Re: [R] Sequence generation in a table
 To: r-help@r-project.org, vincy_p...@yahoo.ca
 Received: Thursday, December 9, 2010, 10:57 AM
 
 Vincy,
 
 I suppose the following does what you want. yy is now a list which 
allows for 
 differing lengths of the vectors.
 
  yy - lapply(c(257, 520, 110), seq, to=0, by=-100)
  yy[[1]]
 [1] 257 157  57
  yy[[2]]
 [1] 520 420 320 220 120  20
 
 
 Regards,
 Jan
 
 
 On 9-12-2010 11:40, Vincy Pyne wrote:
  c(257, 520, 110)
 
 
 
    [[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.




[[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] Sequence generation in a table

2010-12-09 Thread Liviu Andronic
On Thu, Dec 9, 2010 at 1:24 PM, Vincy Pyne vincy_p...@yahoo.ca wrote:
 yy - lapply(c(257, 520, 110), seq, to=0, by=-100)

 yy/360, I get following error.

 Error in yy/360 : non-numeric argument to binary operator

 On the other hand,

 yy[[1]]/365  fetches me

 [1] 0.7138889 0.436 0.158

What about this?
 bond.fun - function(x, days=360) {x/days}
 lapply(yy, bond.fun)
[[1]]
[1] 0.7138889 0.436 0.158

[[2]]
[1] 1. 1.1667 0.8889 0.6111 0. 0.0556

[[3]]
[1] 0.3056 0.0278

 lapply(yy, bond.fun, days=366)
[[1]]
[1] 0.7021858 0.4289617 0.1557377

[[2]]
[1] 1.42076503 1.14754098 0.87431694 0.60109290 0.32786885 0.05464481

[[3]]
[1] 0.30054645 0.02732240

__
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] Sequence generation in a table

2010-12-09 Thread Henrique Dallazuanna
Try this:

 sapply(sapply(c(257, 520, 110), seq, by = -100), '/', 360)


On Thu, Dec 9, 2010 at 10:24 AM, Vincy Pyne vincy_p...@yahoo.ca wrote:

 Dear Sirs,

 I understand these already are numeric values. Sir, Basically I am working
 on Value at Risk for the Bond portfolio using the historical simulation and
 for this I need to find out Marked to Market (MTM) value suing the Present
 Value of the coupon payments for each Bonds (here as an example I have taken
 only 3).

 What I have done so far is for a given bond I have found no of days left
 for maturity. E.g. in 1st case there are 257 days left for maturity. The
 bond pays coupon twice a year and thus on 257th day the bond will mature and
 I will be getting the Principal and final coupon payment. Since teh bond is
 paying the coupons every 6 months, going backward from 257 th day, my
 earlier coupon payment falls on (257 - 180) = 77 days. (However, in above
 example, I have just taken 100 just for example purpose)

 Thus, assuming 100 days, my coupons will be paid on 257, 157,  57days. I
 need to convert these days in terms of years and so when I try to divide yy
 defined as

 yy - lapply(c(257, 520,
  110), seq, to=0, by=-100)

 yy/360, I get following error.

 Error in yy/360 : non-numeric argument to binary operator

 On the other hand,

 yy[[1]]/365  fetches me

 [1] 0.7138889 0.436 0.158


 Thus, I am trying to obtain the result yy - lapply(c(257, 520,
  110), seq, to=0, by=-100) in such a form, so taht I should be able to
 further analysis. What I was trying to say is since here I am taking only
 three bonds, so I can do it individually, however if there are number of
 bonds (say 1000) in the portfolio, my method of converting the days
 individually is not practical.

 I am extremely sorry for the inconvenience caused. I tried to keep my
 problem short in oder not to consume your valuable time.

 Regards

 Vince Pyne



 --- On Thu, 12/9/10, Petr PIKAL petr.pi...@precheza.cz wrote:

 From: Petr PIKAL petr.pi...@precheza.cz
 Subject: Re: [R] Sequence generation in a table
 To: Vincy Pyne vincy_p...@yahoo.ca
 Cc: r-help@r-project.org
 Received: Thursday, December 9, 2010, 12:03 PM

 Hi

 r-help-boun...@r-project.org napsal dne 09.12.2010 12:41:47:

  Dear Sir,
 
  Sorry to bother you again. Sir, the R code provided by you gives me
 following output.
 
   yy - lapply(c(257, 520,
  110), seq, to=0, by=-100)
   yy
  [[1]]
  [1] 257 157  57
 
  [[2]]
  [1] 520 420 320 220 120  20
 
  [[3]]
  [1] 110  10
 
  The biggest constraint for me is here as an example I have taken only
 three
  cases i.e. c(257, 520, 110), however in reality I will be dealing with
 no of
  cases and that number is unknown. But your code will certainly generate
 me the
  required numbers. In above case for doing further calculations, I can
 define say
 
  yy1 = as.numeric(yy[[1]])
  yy2 = as.numeric(yy[2]])
  yy3 = as.numeric(yy[[3]])

 Why? Those values are already numeric.

 lapply(yy, is.numeric)

 [[1]]
 [1] TRUE

 [[2]]
 [1] TRUE

 [[3]]
 [1] TRUE

 and you can use the same construction to perform almost any operation on
 list.

 lapply(yy, max)
 lapply(yy,
  mean)
 lapply(yy, sd)
 lapply(yy, t.test)

 Regards
 Petr


 
  But when the number of cases are unknown, perhaps this is not the
 practical
  way of me defining individually. So is there any way that I can have all
 the
  sequence numbers generated can be accommodated in a single dataframe. I
  sincerely apologize for disturbing you Sir and hope I am able to put up
 my
  problem in a proper manner.
 
  Regards
 
  Vincy Pyne
 
 
  --- On Thu, 12/9/10, Jan van der Laan rh...@eoos.dds.nl wrote:
 
  From: Jan van der Laan rh...@eoos.dds.nl
  Subject: Re: [R] Sequence generation in a table
  To: r-help@r-project.org, vincy_p...@yahoo.ca
  Received: Thursday, December 9, 2010, 10:57 AM
 
  Vincy,
 
  I suppose the following does what you want. yy is now a list which
 allows for
  differing lengths of the vectors.
 
   yy - lapply(c(257, 520, 110), seq, to=0, by=-100)
   yy[[1]]
  [1] 257 157  57
   yy[[2]]
  [1] 520 420 320 220 120  20
 
 
  Regards,
  Jan
 
 
  On 9-12-2010 11:40, Vincy Pyne wrote:
   c(257, 520, 110)
 
 
 
 [[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.




 [[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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[alternative HTML version deleted

Re: [R] Sequence generation in a table

2010-12-09 Thread Petr PIKAL
Hi

r-help-boun...@r-project.org napsal dne 09.12.2010 13:24:17:

 Dear Sirs,
 
 I understand these already are numeric values. Sir, Basically I am 
working on 
 Value at Risk for the Bond portfolio using the historical simulation and 
for 
 this I need to find out Marked to Market (MTM) value suing the Present 
Value 
 of the coupon payments for each Bonds (here as an example I have taken 
only 3). 
 
 What I have done so far is for a given bond I have found no of days left 
for 
 maturity. E.g. in 1st case there are 257 days left for maturity. The 
bond pays
 coupon twice a year and thus on 257th day the bond will mature and I 
will be 
 getting the Principal and final coupon payment. Since teh bond is paying 
the 
 coupons every 6 months, going backward from 257 th day, my earlier 
coupon 
 payment falls on (257 - 180) = 77 days. (However, in above example, I 
have 
 just taken 100 just for example purpose)
 
 Thus, assuming 100 days, my coupons will be paid on 257, 157,  57days. I 
need 
 to convert these days in terms of years and so when I try to divide yy 
defined as 
 
 yy - lapply(c(257, 520,
  110), seq, to=0, by=-100)
 
 yy/360, I get following error.
 
 Error in yy/360 : non-numeric argument to binary operator

Did you bother to read ?lapply or some intro documentation?
 
 On the other hand, 
 
 yy[[1]]/365  fetches me 
 
 [1] 0.7138889 0.436 0.158

I do not believe it. I got

 yy[[1]]/365
[1] 0.7041096 0.4301370 0.1561644

and as I already told you you can use lapply for almost any kind of 
computation. Just try

 lapply(yy, /, 365)
[[1]]
[1] 0.7041096 0.4301370 0.1561644

[[2]]
[1] 1.42465753 1.15068493 0.87671233 0.60273973 0.32876712 0.05479452

[[3]]
[1] 0.30136986 0.02739726

Regards
Petr



 
 
 Thus, I am trying to obtain the result yy - lapply(c(257, 520,
  110), seq, to=0, by=-100) in such a form, so taht I should be able to 
further
 analysis. What I was trying to say is since here I am taking only three 
bonds,
 so I can do it individually, however if there are number of bonds (say 
1000) 
 in the portfolio, my method of converting the days individually is not 
practical.
 
 I am extremely sorry for the inconvenience caused. I tried to keep my 
problem 
 short in oder not to consume your valuable time.
 
 Regards
 
 Vince Pyne
 
 
 
 --- On Thu, 12/9/10, Petr PIKAL petr.pi...@precheza.cz wrote:
 
 From: Petr PIKAL petr.pi...@precheza.cz
 Subject: Re: [R] Sequence generation in a table
 To: Vincy Pyne vincy_p...@yahoo.ca
 Cc: r-help@r-project.org
 Received: Thursday, December 9, 2010, 12:03 PM
 
 Hi
 
 r-help-boun...@r-project.org napsal dne 09.12.2010 12:41:47:
 
  Dear Sir,
  
  Sorry to bother you again. Sir, the R code provided by you gives me 
 following output.
  
   yy - lapply(c(257, 520,
  110), seq, to=0, by=-100)
   yy
  [[1]]
  [1] 257 157  57
  
  [[2]]
  [1] 520 420 320 220 120  20
  
  [[3]]
  [1] 110  10
  
  The biggest constraint for me is here as an example I have taken only 
 three 
  cases i.e. c(257, 520, 110), however in reality I will be dealing with 

 no of 
  cases and that number is unknown. But your code will certainly 
generate 
 me the
  required numbers. In above case for doing further calculations, I can 
 define say 
  
  yy1 = as.numeric(yy[[1]])
  yy2 = as.numeric(yy[2]])
  yy3 = as.numeric(yy[[3]])
 
 Why? Those values are already numeric.
 
 lapply(yy, is.numeric)
 
 [[1]]
 [1] TRUE
 
 [[2]]
 [1] TRUE
 
 [[3]]
 [1] TRUE
 
 and you can use the same construction to perform almost any operation on 

 list.
 
 lapply(yy, max)
 lapply(yy,
  mean)
 lapply(yy, sd)
 lapply(yy, t.test)
 
 Regards
 Petr
 
 
  
  But when the number of cases are unknown, perhaps this is not the 
 practical 
  way of me defining individually. So is there any way that I can have 
all 
 the 
  sequence numbers generated can be accommodated in a single dataframe. 
I 
  sincerely apologize for disturbing you Sir and hope I am able to put 
up 
 my 
  problem in a proper manner.
  
  Regards
  
  Vincy Pyne
  
  
  --- On Thu, 12/9/10, Jan van der Laan rh...@eoos.dds.nl wrote:
  
  From: Jan van der Laan rh...@eoos.dds.nl
  Subject: Re: [R] Sequence generation in a table
  To: r-help@r-project.org, vincy_p...@yahoo.ca
  Received: Thursday, December 9, 2010, 10:57 AM
  
  Vincy,
  
  I suppose the following does what you want. yy is now a list which 
 allows for 
  differing lengths of the vectors.
  
   yy - lapply(c(257, 520, 110), seq, to=0, by=-100)
   yy[[1]]
  [1] 257 157  57
   yy[[2]]
  [1] 520 420 320 220 120  20
  
  
  Regards,
  Jan
  
  
  On 9-12-2010 11:40, Vincy Pyne wrote:
   c(257, 520, 110)
  
  
  
 [[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.
 
 
 
 
[[alternative

Re: [R] Sequence generation

2009-08-31 Thread Petr PIKAL
Hi

r-help-boun...@r-project.org napsal dne 29.08.2009 21:49:54:

 On Sat, Aug 29, 2009 at 8:14 PM, njhuang86njhuan...@yahoo.com wrote:
 
  Hey guys,
 
  I was wondering how to create this sequence: 1, 2, 3, 1, 2, 3, 1, 2, 
3, 1,
  2, 3... with the '1, 2, 3' repeated over 10 times.
 
 rep(1:3,10)  # rep repeats its first argument according to the number
 in its second argument
 
  Also, is there a simple method to generate 1, 1, 1, 2, 2, 2, 3, 3, 3?
 
  The second argument can be a vector, so what you want here is:
 
  rep(1:3,c(3,3,3))

or

rep(1:10,each=3)
 [1]  1  1  1  2  2  2  3  3  3  4  4  4  5  5  5  6  6  6  7  7  7  8  8 
8  9  9  9 10 10 10

Regards
Petr


 
  but you can create the second vector here also using rep! Hence:
 
  rep(1:3,rep(3,3))
 
 Barry
 
 __
 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] Sequence generation

2009-08-29 Thread njhuang86

Hey guys,

I was wondering how to create this sequence: 1, 2, 3, 1, 2, 3, 1, 2, 3, 1,
2, 3... with the '1, 2, 3' repeated over 10 times.

Also, is there a simple method to generate 1, 1, 1, 2, 2, 2, 3, 3, 3?

Anyways, any help with be greatly appreciated!
-- 
View this message in context: 
http://www.nabble.com/Sequence-generation-tp25205593p25205593.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.


Re: [R] Sequence generation

2009-08-29 Thread RIOS,ALFREDO ARTURO


I think should work

rep(c(1,2,3),10)


Alfredo


On Sat Aug 29 15:14:15 EDT 2009, njhuang86 njhuan...@yahoo.com 
wrote:




Hey guys,

I was wondering how to create this sequence: 1, 2, 3, 1, 2, 3, 1, 
2, 3, 1,

2, 3... with the '1, 2, 3' repeated over 10 times.

Also, is there a simple method to generate 1, 1, 1, 2, 2, 2, 3, 
3, 3?


Anyways, any help with be greatly appreciated!
-- View this message in context: 
http://www.nabble.com/Sequence-generation-tp25205593p25205593.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.







--
RIOS,ALFREDO ARTURO

__
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] Sequence generation

2009-08-29 Thread David Winsemius


On Aug 29, 2009, at 3:14 PM, njhuang86 wrote:



Hey guys,

I was wondering how to create this sequence: 1, 2, 3, 1, 2, 3, 1, 2,  
3, 1,

2, 3... with the '1, 2, 3' repeated over 10 times.


As noted earlier

rep(1:3, 10)



Also, is there a simple method to generate 1, 1, 1, 2, 2, 2, 3, 3, 3?


?gl

the gl function will return a factor and it can be converted to a  
vector:


 gl(10, 3)
 [1] 1  1  1  2  2  2  3  3  3  4  4  4  5  5  5  6  6  6  7  7  7   
8  8  8  9  9  9  10 10 10

Levels: 1 2 3 4 5 6 7 8 9 10
 as.numeric(gl(10,3))
 [1]  1  1  1  2  2  2  3  3  3  4  4  4  5  5  5  6  6  6  7  7  7   
8  8  8  9  9  9 10 10 10


Or you can cobble something together like:
 floor(1 + 0:29 / 3)
 [1]  1  1  1  2  2  2  3  3  3  4  4  4  5  5  5  6  6  6  7  7  7   
8  8  8  9  9  9 10 10 10


which depends on the higher precedence of the : operator over  
infix-+ and /.


?Syntax

--

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
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] Sequence generation

2009-08-29 Thread Barry Rowlingson
On Sat, Aug 29, 2009 at 8:14 PM, njhuang86njhuan...@yahoo.com wrote:

 Hey guys,

 I was wondering how to create this sequence: 1, 2, 3, 1, 2, 3, 1, 2, 3, 1,
 2, 3... with the '1, 2, 3' repeated over 10 times.

rep(1:3,10)  # rep repeats its first argument according to the number
in its second argument

 Also, is there a simple method to generate 1, 1, 1, 2, 2, 2, 3, 3, 3?

 The second argument can be a vector, so what you want here is:

 rep(1:3,c(3,3,3))

 but you can create the second vector here also using rep! Hence:

 rep(1:3,rep(3,3))

Barry

__
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] Sequence generation

2009-08-29 Thread Greg Snow
Or just use the 'each' argument to seq.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.s...@imail.org
801.408.8111


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-
 project.org] On Behalf Of Barry Rowlingson
 Sent: Saturday, August 29, 2009 1:50 PM
 To: njhuang86
 Cc: r-help@r-project.org
 Subject: Re: [R] Sequence generation
 
 On Sat, Aug 29, 2009 at 8:14 PM, njhuang86njhuan...@yahoo.com wrote:
 
  Hey guys,
 
  I was wondering how to create this sequence: 1, 2, 3, 1, 2, 3, 1, 2,
 3, 1,
  2, 3... with the '1, 2, 3' repeated over 10 times.
 
 rep(1:3,10)  # rep repeats its first argument according to the number
 in its second argument
 
  Also, is there a simple method to generate 1, 1, 1, 2, 2, 2, 3, 3, 3?
 
  The second argument can be a vector, so what you want here is:
 
  rep(1:3,c(3,3,3))
 
  but you can create the second vector here also using rep! Hence:
 
  rep(1:3,rep(3,3))
 
 Barry
 
 __
 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.