How to keep function(dset,group) unchanged in R -
this question has answer here:
x<- c(62, 60, 63, 59, 63, 67) grp1<-factor(rep(1:2)) grp2<-rep(1:3) dat <-data.frame(x,grp1,grp2) aaa<-function(dset,group) { if (length(levels(group))==2) { print("ccc") } else { print("ddd") } }
i run aaa(dset=dat,group="grp1")
, result not "ccc"
. how revise aaa
function contents , keep aaa(dset=dat,group="grp1")
unchanged?
my answer:
aaa<-function(dset,group) { grp<-dset[,c(group)] if (length(levels(grp))==2) { print("ccc") } else { print("ddd") } } aaa(dset=dat,group="grp1")
the function needs know context of group
(namely subset of dset
):
aaa <- function(dset,group) { if (length(levels(dset[,group])) == 2) { #this different print("ccc") } else { print("ddd") } }
Comments
Post a Comment