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")
, but the result is not "ccc"
. How to revise the aaa
function contents and 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")