我想在R中创建一个函数,根据分类变量(一个因子)的级别创建一个数据框的子集。最终,我的功能将操作这个子集,但我不能让第一部分的工作....在R中创建函数:使用因子级别作为参数?
这里是我的代码,结果我得到的,当我使用的功能:
> Petite13.b [1:5, ]
Numero Espece Arbre Nb
1 1 BOP Brout_mort 1
61 1 BOP Mutile 2
130 1 SAB Mutile 1
213 1 BOP Vivant 1
439 1 SAB Vivant 2
> Creation.PLL <- function(Esp, Arb, Source){
+ x <-Source[Source$Espece== "Esp" & Source$Arbre== "Arb", ]
+ return(x)
+ }
>
> Creation.PLL(SAB, Vivant, Petite13.b)
[1] Numero Espece Arbre Nb
<0 lignes> (ou 'row.names' de longueur nulle)
我的数据帧,这里名为Source
将始终有一个名为Source$Espece
的变量,另一个名为Source$Arbre
。
谢谢。
你是不是要做'Source [Source $ Espece == Esp&Source $ Arbre == Arb,]'(其中'Esp'和'Arb'没有引用)? – nrussell 2014-09-26 14:33:30
请发布一个小的*可重现的*您的数据样本。 – 2014-09-26 14:35:40
@nrussell即使我把“Esp”放在函数编程和使用它之后,它也不起作用。 – Emilie 2014-09-26 14:39:26