vector with specified values construction

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

vector with specified values construction

PIKAL Petr
Dear all

I want to have vector of length 24 which is filled by zeroes except
that on position

c(8,16,19,20) is 2
and
c(3,4,12,24) is 4
instead.
 
I am able to do

> (1:24)%in%c(8,16,19,20)*2
 [1] 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 2 2 0 0 0 0

and

> (1:24)%in%c(3,4,12,24)*4
 [1] 0 0 4 4 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 4

but I can not find a suitable construction to end with vector
0 0 4 4 0 0 0 2 0 0 0 4 0 0 0 2 0 0 2 2 0 0 0 4

Can anybody help please?


Petr Pikal
[hidden email]

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

Re: vector with specified values construction

Dimitris Rizopoulos
consider the following:

x <- numeric(24)
x[c(8,16,19,20)] <- 2
x[c(3,4,12,24)] <- 4
x

I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message -----
From: "Petr Pikal" <[hidden email]>
To: <[hidden email]>
Sent: Tuesday, January 31, 2006 2:56 PM
Subject: [R] vector with specified values construction


> Dear all
>
> I want to have vector of length 24 which is filled by zeroes except
> that on position
>
> c(8,16,19,20) is 2
> and
> c(3,4,12,24) is 4
> instead.
>
> I am able to do
>
>> (1:24)%in%c(8,16,19,20)*2
> [1] 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 2 2 0 0 0 0
>
> and
>
>> (1:24)%in%c(3,4,12,24)*4
> [1] 0 0 4 4 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 4
>
> but I can not find a suitable construction to end with vector
> 0 0 4 4 0 0 0 2 0 0 0 4 0 0 0 2 0 0 2 2 0 0 0 4
>
> Can anybody help please?
>
>
> Petr Pikal
> [hidden email]
>
> ______________________________________________
> [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
>


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

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

Re: vector with specified values construction

PIKAL Petr
In reply to this post by PIKAL Petr
Thanks to all who responded. I definitly need some fresh air to clean
my head. :-)

Petr

On 31 Jan 2006 at 14:56, Petr Pikal wrote:

From:           "Petr Pikal" <[hidden email]>
To:             [hidden email]
Date sent:       Tue, 31 Jan 2006 14:56:47 +0100
Priority:       normal
Subject:         [R] vector with specified values construction

> Dear all
>
> I want to have vector of length 24 which is filled by zeroes except
> that on position
>
> c(8,16,19,20) is 2
> and
> c(3,4,12,24) is 4
> instead.
>
> I am able to do
>
> > (1:24)%in%c(8,16,19,20)*2
>  [1] 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 2 2 0 0 0 0
>
> and
>
> > (1:24)%in%c(3,4,12,24)*4
>  [1] 0 0 4 4 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 4
>
> but I can not find a suitable construction to end with vector
> 0 0 4 4 0 0 0 2 0 0 0 4 0 0 0 2 0 0 2 2 0 0 0 4
>
> Can anybody help please?
>
>
> Petr Pikal
> [hidden email]
>
> ______________________________________________
> [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

Petr Pikal
[hidden email]

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