[R] Plotting using 'if' statements

Steve Murray smurray444 at hotmail.com
Thu Sep 4 18:28:31 CEST 2008

Dear all,

I have a dataset of four columns, and I wish to plot (as a scatter graph) the values of the third column where the values are greater than zero, and the fourth column. 

I tried doing this via the plot command itself, but got into a bit of a mess (resulting in errors!). My dataframe is called 'January':

> plot(January[3(>0):4])
Error: unexpected '>' in "plot(January[3(>"

After a few variations on this, I thought I'd try making a new object which includes all values from the third column of January>0 (to plot in a separate step) as follows:

> JanFilter <- January[3]>0

No error here. However, when I display the 'values' of JanFilter, it shows that instead of keeping the numerical values, the above operation simply displays the results of the logical test:

> head(JanFilter)
[1,]                       FALSE
[2,]                        TRUE
[3,]                        TRUE
[4,]                        TRUE
[5,]                        TRUE
[6,]                        TRUE

This is obviously no good for plotting the numerical values on axes!

So my question is, how do I perform 'if' statements in order to filter out various parts of a dataset, for plotting on a graph.

Many thanks,


More information about the R-help mailing list