1
votes

Within the R programming language, is it possible to use a where clause within ggplot? If not, then within the R language to allow me to use a where clause? For example, using the diamonds table can I only select records that the cut = "Good". Cut is a field in the Diamond table. The Cut field can equal "Fair", "Good", Very Good", "Premium", and "Ideal".

diamonds[1:5,]
#   carat     cut   color  clarity    depth  table   price         x    y    z
# 1  0.23   Ideal       E      SI2     61.5     55     326      3.95 3.98 2.43  

I want to use some type of conditional statement against a CSV read file. But to keep my example simple, I using the diamond table as a typical example of my problem. I want to filter cut = "Good" but to include fields related to the records = "Good".

Thank you for any help to my question.

1
what about ggplot(subset(diamonds,cut=="Good")) + ... ?Ben Bolker
if you prefer SQL syntax you might look at the sqldf package.Ben Bolker
just don't use "where," it is not needed. the bigger picture is that you need subset methods which r has in spades: subset,[,[[,$, etcrawr

1 Answers

7
votes

You can use the subset function to filter rows from a data frame:

goodDiamods <- subset(diamonds, cut=='Good')

Check: Quick-R: Subsetting data


Another option is to use the sqldf package, and write a "query-like" expression:

library(sqldf)
goodDiamonds <- sqldf("select * from diamonds where cut='Good'")