[R] Vector manipulation, for loop needed?

David Winsemius dwinsemius at comcast.net
Fri Jun 18 22:42:04 CEST 2010


On Jun 18, 2010, at 10:52 AM, clips10 wrote:

>
> Hi,
>
> I have a vector of time in days t<-1:48.
>
> I have observations and the day they were recorded. I also have a  
> vector, S
> which takes different values depending on which day the observation  
> was
> recorded. For example if on day 1 all in vector S get a value of  
> 46/48, on
> day 2 get 42/48, day 3 38/48, decreasing by 4/48 each time until day  
> 12 gets
> the value 2/48. Then for any day greater than 12 the vector S has  
> value 0.
>
> I want to create a vector which gives the values of S in terms of  
> days, t.
> I have tried:
>
> t<-1:48
> for(i in 1:t){
> S<-rep(0,48)
> if(t<12){
> S[i]<-(48-3*t)/48}
> else{
> S[i]<-0}
> }

 > t<- 1:48; x <- ifelse( (50-t*4)> 0 , (50-t*4)/48, 0) ; x
  [1] 0.95833333 0.87500000 0.79166667 0.70833333 0.62500000 0.54166667
  [7] 0.45833333 0.37500000 0.29166667 0.20833333 0.12500000 0.04166667
[13] 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
[19] 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
[25] 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
[31] 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
[37] 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
[43] 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000


>
> but this doesn't work. I'm fairly new to R and don't have much skill  
> in
> manipulating things. I'm trying to plot time against this vector so  
> need it
> in terms of t. Sorry that this isn't even very clear in what I want  
> and I'm
> aware that the (48-3*t)/48 won't even give me the
> 46/48,42/48,38/48....2/48,0....0 pattern I'm after but no idea what  
> to do!

Close, just not quite there. Try:

  (50-4*t)/48

>
> Any help would be great.
> Thanks


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list