-1

I have following data

a<- rnorm(10)*10+100
b<- c(0,0,0,1,1,0,1,0,1,1)

First i would like to plot a line with

plot(a)

And then i would like to plot points wherever there is 1 in the series b .

Please help me. I am really stuck.

prabinseth
  • 33
  • 4
  • 1
    why down vote guys ? i am very new in R. i only know few commands in R. I searched the forum for possible clues. but failed. If this question is answered before please give me reference. – prabinseth Jul 04 '14 at 08:47

1 Answers1

1

Using ifelse you can choose value in vector "a" to be plotted:

ifelse(b==1,a,0)

To get lines (not the default points) in the first plot , you should use type="l".

The code can be something like this :

set.seed(1)   ## I set the seed here to get a reproducible example
a<- rnorm(10)*10+100
b<- c(0,0,0,1,1,0,1,0,1,1)
plot(a,type='l')
points(ifelse(b==1,a,0),col='red',pch=20,cex=2)

enter image description here

agstudy
  • 119,832
  • 17
  • 199
  • 261