15

Is there a simple way to do this in R:

plot(var1,var2, for all observations in the data frame where var3 < 155)

It is possible by creating a new data newdata <- data[which( data$var3 < 155),] but then I have to redefine all the variables newvar1 <- newdata$var1 etc.

mbq
  • 18,510
  • 6
  • 49
  • 72
jinni
  • 373
  • 1
  • 3
  • 12
  • You don't need `which` -- in fact you shouldn't use it from performance reasons. It converts bool index to numerical one, yet you can use both equally well to subset the data frame. – mbq Nov 28 '11 at 17:35

4 Answers4

28

with(dfr[dfr$var3 < 155,], plot(var1, var2)) should do the trick.

Edit regarding multiple conditions:

with(dfr[(dfr$var3 < 155) & (dfr$var4 > 27),], plot(var1, var2))
Nick Sabbe
  • 11,684
  • 1
  • 43
  • 57
  • Thanks for the quick reply. I tried this but it just outputs the plot as if the restriction var3<155 is not there. – jinni Nov 28 '11 at 09:19
  • Well, I tried it with a data.frame created like this: `dfr<-data.frame(var1=rnorm(100), var2=rnorm(100), var3=rnorm(100, 160, 10))`, and it worked flawlessly. Perhaps something else is in your way? Or perhaps all your observations have var3 < 155 ? – Nick Sabbe Nov 28 '11 at 09:35
  • It worked,thanks. (my problem was I had already defined var1 and var2 as the full vectors earlier). Also, Is it possible to have more than one condition, e.g var4 > 27 ? – jinni Nov 28 '11 at 10:03
  • 4
    or `with(subset(df,var3<155 & var4>27), plot(var1,var2))` – Ben Bolker Nov 28 '11 at 14:24
12

Most straightforward option:

plot(var1[var3<155],var2[var3<155])

It does not look good because of code redundancy, but is ok for fastndirty hacking.

mbq
  • 18,510
  • 6
  • 49
  • 72
4

This is how I would do it, in order to get in the var4 restriction:

dfr<-data.frame(var1=rnorm(100), var2=rnorm(100), var3=rnorm(100, 160, 10), var4=rnorm(100, 27, 6))
plot( subset( dfr, var3 < 155 & var4 > 27, select = c( var1, var2 ) ) )

Rgds, Rainer

vaettchen
  • 7,299
  • 22
  • 41
  • Thanks, it works. Is it possible to plot functions of the variables in this way? e.g. log(var1) or var1/var2 – jinni Nov 28 '11 at 10:29
  • @jamie314 - not sure what you exactly want to achieve but I think you have to assign the subset to a new variable first `dfr1 <- subset( dfr, var3 < 155 & var4 > 27, select = c( var1, var2 ) )`, then you can do stuff like `plot( dfr1$var2, dfr1$var1 / dfr1$var2 )` – vaettchen Nov 28 '11 at 10:48
1

This chunk should do the work:

plot(var2 ~ var1, data=subset(dataframe, var3 < 150))

My best regards.

How this works:

  1. Fisrt, we make selection using the subset function. Other possibilities can be used, like, subset(dataframe, var4 =="some" & var5 > 10). The "&" operator can be used to select all "some" and over 10. Also the operator "|" could be used to select "some" or "over 10".
  2. The next step is to plot the results of the subset, using tilde (~) operator, that just imply a formula, in this case var.response ~ var.independet. Of course this is not a formula, but works great for this case.
André WZ
  • 141
  • 4
  • 1
    This looks like a viable solution, however, you might add a comment as to why it works. I personally prefer using `[` to `subset` as it reduces the potential of side effects. – lmo Sep 03 '16 at 18:12