Arielle Saunders
Arielle Saunders

Reputation: 51

R Function Query

Write a function called "query". It takes five inputs

and returns a data frame which contains the rows of df where the reviewer's age is between "age_low" and "age_high" (inclusive),the review text contains "keyword" (case insensitive), and the rating is between "rating_low" and "rating_high" (inclusive). Save the output of query(age_low=20, age_high=50) in df1 Save the output of query(rating_high=3) in df2 Save the output of query(age_low=20, age_high=50, keyword="price", rating_low=4, rating_high=5) in df3

query <- function(age_low=1, age_high=100, keyword="", rating_low=1, rating_high=5) {
  return(df$Age>= age_low and df$Age<=age_high,
         tolower(df$`Review Text` == tolower(keyword)),
         df$Rating>=rating_low and df$Rating<=rating_high)
}

df1 <- query(age_low=20, age_high=50)
df2 <- query(rating_high=3) 
df3 <- query(age_low=20, age_high=50, keyword="price", rating_low=4, rating_high=5)

Upvotes: 1

Views: 146

Answers (1)

Fujibayashi Kyou
Fujibayashi Kyou

Reputation: 79

Except for the criteria, you should also put df as an input for your function as a good habit.

query <- function(df=df, age_low=1, age_high=100, keyword="", rating_low=1, rating_high=5) {
  # first filter
  out_df <- df[df$Age>=age_low & df$Age<=age_high, ]

  # second filter
  out_df <- out_df[tolower(out_df$`Review Text`) == tolower(keyword), ]

  # third filter
  out_df <- out_df[out_df$Rating>=rating_low & out_df$Rating<=rating_high, ]

  return(out_df)
}

df1 <- query(age_low=20, age_high=50)
df2 <- query(rating_high=3) 
df3 <- query(age_low=20, age_high=50, keyword="price", rating_low=4, rating_high=5)

Upvotes: 1

Related Questions