我有一个名为df
的示例数据框:
Store Cash Only
1 "A" Y
2 "B" N
3 "C" N
4 "D" Y
我想创建一个允许用户查看商店是否“仅限现金”的功能。理想情况下,该函数仅输出Y
列中Cash Only
的商店。输出应如下所示:
Store Cash Only
1 "A" Y
2 "D" Y
有谁知道我会怎么做?谢谢!
答案 0 :(得分:2)
你只需编写一个函数来返回输入数据框的子集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)
答案 1 :(得分:2)
此功能为您提供更大的灵活性,
select_sub_dataframe&LT; -function(DF,colname的,值){ 返回(DF [DF [,colname的] ==值,]) }
您可以调用此函数并通过
获取子集select_sub_dataframe(df,“仅限现金”,“是”)