Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot a subset of a data frame in R?

Tags:

plot

r

subset

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.

like image 903
jinni Avatar asked Nov 28 '11 09:11

jinni


People also ask

How do I display a subset of data in R?

If you wanted to get the subset of a data. frame (DataFrame) Rows & Columns in R, either use the subset() function , filter() from dplyr package or R base square bracket notation df[] . subset() is a generic R function that is used to get the rows and columns (In R terms observations & variables) from the data frame.

What is the use of subset () and sample () function in R?

The difference between subset () function and sample () is that, subset () is used to select data from the dataset which meets certain condition, while sample () is used for randomly selecting data of size 'n' from the dataset. This recipe demonstrates an example on subset () and sample () in R.


1 Answers

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))
like image 163
Nick Sabbe Avatar answered Sep 26 '22 08:09

Nick Sabbe