Determining Index of Last Element in Vector

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Determining Index of Last Element in Vector

Alan Lue
Hi,

Is there a way to specify the last element of a vector, similar to "end" in
MATLAB?

  v[end]

would be MATLAB for

  v(length(v))

in R.

While `v(length(v))' does yield the last element, that approach fails in the
following,

  rep(v, each=2)[-c(1,length(v))]

which is meant to duplicate all elements of `v' except for the first and
last.  (I.e., if `v <- 1:4', then we want '1 2 2 3 3 4'.)

So the question is, is there a better way specify the last element of a
vector?  If not, is there a better way to duplicate all elements of a vector
except for the first and last?  (I know you can achieve this using two
lines, but I'm writing because I want to do it using one.)

Alan

--
Alan Lue
Master of Financial Engineering
UCLA Anderson School of Management

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Determining Index of Last Element in Vector

Alan Lue
Sorry -- I meant `v(end)' and `v[length(v)]' in the first examples of
my message.

Alan


On Sun, Apr 25, 2010 at 11:10 AM, Alan Lue <[hidden email]> wrote:

> Hi,
> Is there a way to specify the last element of a vector, similar to "end" in
> MATLAB?
>   v[end]
> would be MATLAB for
>   v(length(v))
> in R.
> While `v(length(v))' does yield the last element, that approach fails in the
> following,
>   rep(v, each=2)[-c(1,length(v))]
> which is meant to duplicate all elements of `v' except for the first and
> last.  (I.e., if `v <- 1:4', then we want '1 2 2 3 3 4'.)
> So the question is, is there a better way specify the last element of a
> vector?  If not, is there a better way to duplicate all elements of a vector
> except for the first and last?  (I know you can achieve this using two
> lines, but I'm writing because I want to do it using one.)
> Alan
>
> --
> Alan Lue
> Master of Financial Engineering
> UCLA Anderson School of Management
>



--
Alan Lue
Master of Financial Engineering
UCLA Anderson School of Management

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Determining Index of Last Element in Vector

Jorge I Velez
In reply to this post by Alan Lue
Hi Alan,

Take a look at the following:

> x <- 1:10
> x[length(x)]
[1] 10
> tail(x)
[1]  5  6  7  8  9 10
> tail(x, 1)
[1] 10

See ?tail for more information.

HTH,
Jorge


On Sun, Apr 25, 2010 at 2:10 PM, Alan Lue <> wrote:

> Hi,
>
> Is there a way to specify the last element of a vector, similar to "end" in
> MATLAB?
>
>  v[end]
>
> would be MATLAB for
>
>  v(length(v))
>
> in R.
>
> While `v(length(v))' does yield the last element, that approach fails in
> the
> following,
>
>  rep(v, each=2)[-c(1,length(v))]
>
> which is meant to duplicate all elements of `v' except for the first and
> last.  (I.e., if `v <- 1:4', then we want '1 2 2 3 3 4'.)
>
> So the question is, is there a better way specify the last element of a
> vector?  If not, is there a better way to duplicate all elements of a
> vector
> except for the first and last?  (I know you can achieve this using two
> lines, but I'm writing because I want to do it using one.)
>
> Alan
>
> --
> Alan Lue
> Master of Financial Engineering
> UCLA Anderson School of Management
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Determining Index of Last Element in Vector

Chuck Cleland
In reply to this post by Alan Lue
On 4/25/2010 2:10 PM, Alan Lue wrote:

> Hi,
>
> Is there a way to specify the last element of a vector, similar to "end" in
> MATLAB?
>
>   v[end]
>
> would be MATLAB for
>
>   v(length(v))
>
> in R.
>
> While `v(length(v))' does yield the last element, that approach fails in the
> following,
>
>   rep(v, each=2)[-c(1,length(v))]
>
> which is meant to duplicate all elements of `v' except for the first and
> last.  (I.e., if `v <- 1:4', then we want '1 2 2 3 3 4'.)

v <- 1:4

rep(v, c(1, rep(2, length(v) - 2), 1))
[1] 1 2 2 3 3 4

> So the question is, is there a better way specify the last element of a
> vector?  If not, is there a better way to duplicate all elements of a vector
> except for the first and last?  (I know you can achieve this using two
> lines, but I'm writing because I want to do it using one.)
>
> Alan

--
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Determining Index of Last Element in Vector

David Winsemius

On Apr 25, 2010, at 2:22 PM, Chuck Cleland wrote:

> On 4/25/2010 2:10 PM, Alan Lue wrote:
>> Hi,
>>
>> Is there a way to specify the last element of a vector, similar to  
>> "end" in
>> MATLAB?
>>
>>  v[end]
>>
>> would be MATLAB for
>>
>>  v(length(v))
>>
>> in R.
>>
>> While `v(length(v))' does yield the last element, that approach  
>> fails in the
>> following,
>>
>>  rep(v, each=2)[-c(1,length(v))]

Cleland's example works, but I thought you might be interested in  
ideas about why your approach did not. The length of you rep-ed vector  
was twice as long as the original so this method, which only adds a  
factor of two to the length argument, succeeds:

 >  rep(v, each=2)[-c(1,2*length(v))]
[1] 1 2 2 3 3 4

--
David.

>>
>> which is meant to duplicate all elements of `v' except for the  
>> first and
>> last.  (I.e., if `v <- 1:4', then we want '1 2 2 3 3 4'.)
>
> v <- 1:4
>
> rep(v, c(1, rep(2, length(v) - 2), 1))
> [1] 1 2 2 3 3 4
>
>> So the question is, is there a better way specify the last element  
>> of a
>> vector?  If not, is there a better way to duplicate all elements of  
>> a vector
>> except for the first and last?  (I know you can achieve this using  
>> two
>> lines, but I'm writing because I want to do it using one.)
>>
>> Alan
>
> --
> Chuck Cleland, Ph.D.
> NDRI, Inc. (www.ndri.org)
> 71 West 23rd Street, 8th floor
> New York, NY 10010
> tel: (212) 845-4495 (Tu, Th)
> tel: (732) 512-0171 (M, W, F)
> fax: (917) 438-0894
>
> ______________________________________________
> [hidden email] 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
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Determining Index of Last Element in Vector

Alan Lue
Those are great solutions.  Thanks so much for your help.

Yours,
Alan


On Sun, Apr 25, 2010 at 11:43 AM, David Winsemius
<[hidden email]> wrote:

>
> On Apr 25, 2010, at 2:22 PM, Chuck Cleland wrote:
>
>> On 4/25/2010 2:10 PM, Alan Lue wrote:
>>>
>>> Hi,
>>>
>>> Is there a way to specify the last element of a vector, similar to "end"
>>> in
>>> MATLAB?
>>>
>>>  v[end]
>>>
>>> would be MATLAB for
>>>
>>>  v(length(v))
>>>
>>> in R.
>>>
>>> While `v(length(v))' does yield the last element, that approach fails in
>>> the
>>> following,
>>>
>>>  rep(v, each=2)[-c(1,length(v))]
>
> Cleland's example works, but I thought you might be interested in ideas
> about why your approach did not. The length of you rep-ed vector was twice
> as long as the original so this method, which only adds a factor of two to
> the length argument, succeeds:
>
>>  rep(v, each=2)[-c(1,2*length(v))]
> [1] 1 2 2 3 3 4
>
> --
> David.
>
>>>
>>> which is meant to duplicate all elements of `v' except for the first and
>>> last.  (I.e., if `v <- 1:4', then we want '1 2 2 3 3 4'.)
>>
>> v <- 1:4
>>
>> rep(v, c(1, rep(2, length(v) - 2), 1))
>> [1] 1 2 2 3 3 4
>>
>>> So the question is, is there a better way specify the last element of a
>>> vector?  If not, is there a better way to duplicate all elements of a
>>> vector
>>> except for the first and last?  (I know you can achieve this using two
>>> lines, but I'm writing because I want to do it using one.)
>>>
>>> Alan
>>
>> --
>> Chuck Cleland, Ph.D.
>> NDRI, Inc. (www.ndri.org)
>> 71 West 23rd Street, 8th floor
>> New York, NY 10010
>> tel: (212) 845-4495 (Tu, Th)
>> tel: (732) 512-0171 (M, W, F)
>> fax: (917) 438-0894
>>
>> ______________________________________________
>> [hidden email] 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
> West Hartford, CT
>
>



--
Alan Lue
Master of Financial Engineering
UCLA Anderson School of Management

______________________________________________
[hidden email] 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.