Reputation: 373
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.
Upvotes: 15
Views: 123338
Reputation: 141
This chunk should do the work:
plot(var2 ~ var1, data=subset(dataframe, var3 < 150))
My best regards.
How this works:
Upvotes: 1
Reputation: 18628
Most straightforward option:
plot(var1[var3<155],var2[var3<155])
It does not look good because of code redundancy, but is ok for fastn
dirty hacking.
Upvotes: 12
Reputation: 11956
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))
Upvotes: 28
Reputation: 7659
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
Upvotes: 4