Ashley Ashley - 4 years ago 78
R Question

function that outputs a data frame in R

I have an example data frame named

df
:

Store Cash Only
1 "A" Y
2 "B" N
3 "C" N
4 "D" Y


I would like to create a function that allows a user to see whether or not a store is "cash only". Ideally, the function would output only the stores that have a
Y
in the
Cash Only
column. The output should look like this:

Store Cash Only
1 "A" Y
2 "D" Y


Does anyone know how I would go about this? Thanks!

Answer Source

you just need to write a function that returns a subset of the input dataframe where cash == "Y":

df = data.frame(store=c("a","b","c","d"), cash=c("Y","N","N","Y"))

cash_only <- function(df){
  return(subset(df, cash == "Y"))
}

new_df <- cash_only(df)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download