就像一个示例,创建一个类似于
countif
在Excel中,我尝试在下面的ddply“countif”变量定义中使用字符串“mycolumn”:
df <- c("a","a","b","c","c") %>% data.frame(stringsAsFactors = F)
colnames(df) <- "mycolumn"
x <- "mycolumn"
countif <- function(df,x ) {
y <- which(colnames(df)==x)
result1 <- ddply(df,x,nrow) #this works, but I can't use the x argument
result2 <- ddply(df,x,summarise, countif=length(df[,y])) #not working
result3 <- ddply(df,x,summarise, countif=length(parse(text=x))) #not working
}
正如你在下面看到的,只有
result1
有效,但我需要一种方法来使用我的
mycolumn
ddply函数中的字符串,而不是单独依赖
nrow
. 非常感谢。
> result1
mycolumn V1
1 a 2
2 b 1
3 c 2
> result2
mycolumn countif
1 a 5
2 b 5
3 c 5
> result3
mycolumn countif
1 a 1
2 b 1
3 c 1