Re: [R] How to insert one element into a vector?

2004-11-23 Thread Jari Oksanen
On Mon, 2004-11-22 at 17:43, Barry Rowlingson wrote:
 Deepayan Sarkar wrote:
 
  Pretty much what 'append' does.
 
   A shame then, that help.search(insert) doesn't find 'append'! I cant 
 think why anyone looking for a way of _inserting_ a value in the middle 
 of a vector would think of looking at append!
 
   Python has separate insert and append methods for vectors.
 
x=[1,2,3,4,6]
x.insert(4,5)
x
   [1, 2, 3, 4, 5, 6]
x.append(99)
x
   [1, 2, 3, 4, 5, 6, 99]

So has R. R's 'insert' is called 'append', and R's 'append' is called
'c'. Counter-intuitively though, and I'm happy that Peter Dalgaard
didn't know that 'append' inserts: it gives some hope to us ordinary
mortals.

cheers, jazza
-- 
Jari Oksanen [EMAIL PROTECTED]

__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


RE: [R] How to insert one element into a vector?

2004-11-22 Thread michael watson (IAH-C)
There must be a million ways of doing this!
If you want to keep the order:

v - c(1,2,3,4,6)
sort(c(v,5))

Mick

-Original Message-
From: jing tang [mailto:[EMAIL PROTECTED] 
Sent: 22 November 2004 14:44
To: [EMAIL PROTECTED]
Subject: [R] How to insert one element into a vector?


suppose I want to insert 5 into the vector (1,2,3,4,6) between 4 and 6.
thx!

__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] How to insert one element into a vector?

2004-11-22 Thread Barry Rowlingson
michael watson (IAH-C) wrote:
There must be a million ways of doing this!
 Actually I'd say there were no ways of doing this, since I dont think 
you can actually insert into a vector - you have to create a new vector 
that produces the illusion of insertion!

 Here's a Q+D function that fails if you try and insert at the start, 
or at the end. Its very D.

insert - function(v,e,pos){
  return(c(v[1:(pos-1)],e,v[(pos):length(v)]))
}
  v=c(1,2,3,4,6)
  insert(v,5,5)
 [1] 1 2 3 4 5 6
Yay.
  insert(v,5,1)
 [1] 1 5 1 2 3 4 6
Oops.
 Cant be bothered to fix it, the principle is there.
Baz
__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] How to insert one element into a vector?

2004-11-22 Thread Roger D. Peng
?append --- look at the `after' argument.
-roger
jing tang wrote:
suppose I want to insert 5 into the vector (1,2,3,4,6) between 4 and 6.
thx!
__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! 
http://www.R-project.org/posting-guide.html

--
Roger D. Peng
http://www.biostat.jhsph.edu/~rpeng/
__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] How to insert one element into a vector?

2004-11-22 Thread Deepayan Sarkar
On Monday 22 November 2004 09:19, Barry Rowlingson wrote:
 michael watson (IAH-C) wrote:
  There must be a million ways of doing this!

   Actually I'd say there were no ways of doing this, since I dont
 think you can actually insert into a vector - you have to create a
 new vector that produces the illusion of insertion!

   Here's a Q+D function that fails if you try and insert at the
 start, or at the end. Its very D.

 insert - function(v,e,pos){
return(c(v[1:(pos-1)],e,v[(pos):length(v)]))
 }

Pretty much what 'append' does.

Deepayan

__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] How to insert one element into a vector?

2004-11-22 Thread Peter Dalgaard
Barry Rowlingson [EMAIL PROTECTED] writes:

 michael watson (IAH-C) wrote:
  There must be a million ways of doing this!
 
   Actually I'd say there were no ways of doing this, since I dont
 think you can actually insert into a vector - you have to create a new
 vector that produces the illusion of insertion!
 
   Here's a Q+D function that fails if you try and insert at the start,
 or at the end. Its very D.
 
 insert - function(v,e,pos){
return(c(v[1:(pos-1)],e,v[(pos):length(v)]))
 }

This is actually an exercise in a certain introductory book on R, with
the intention of having the student do the obvious c(v[1:4],5,v[5:5])
style computation and maybe think a little about the edge effects. I
only recently saw the somewhat unfortunately named append() function,
which has been in S/R ever since the blue book...:

 append(v,5,4)
[1] 1 2 3 4 5 6


-- 
   O__   Peter Dalgaard Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics 2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark  Ph: (+45) 35327918
~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907

__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] How to insert one element into a vector?

2004-11-22 Thread Barry Rowlingson
Deepayan Sarkar wrote:
Pretty much what 'append' does.
 A shame then, that help.search(insert) doesn't find 'append'! I cant 
think why anyone looking for a way of _inserting_ a value in the middle 
of a vector would think of looking at append!

 Python has separate insert and append methods for vectors.
  x=[1,2,3,4,6]
  x.insert(4,5)
  x
 [1, 2, 3, 4, 5, 6]
  x.append(99)
  x
 [1, 2, 3, 4, 5, 6, 99]
Barry
__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] How to insert one element into a vector?

2004-11-22 Thread Thomas Lumley
On Mon, 22 Nov 2004, Barry Rowlingson wrote:
Deepayan Sarkar wrote:
Pretty much what 'append' does.
A shame then, that help.search(insert) doesn't find 'append'! I cant think 
why anyone looking for a way of _inserting_ a value in the middle of a vector 
would think of looking at append!
Yes, this should be fixed.
Python has separate insert and append methods for vectors.
I don't think this is a good idea unless there are efficiency issues. 
You could always define one: the definition is fairly simple.
   insert-append

-thomas
__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html