Peter
Peter

Reputation: 13

summaryBy doesn't work properly

I want to count empty rowcounts of variable Comment. There are two empty cells so the rowcount should be 2.

Below is my script on the sample data.

Temp data:
 Comment       Prob
1      fa 0.40768666
2     fsa 0.61956024
3                 NA
4         0.12916298
5   fasdf 0.09724928
6     rew 0.47395962

c_fun <- function(x){c(example=head(x,n=1),mcnt=sum(as.character(x)==""))}
summaryBy(Comment~., data= temp, FUN= c_fun)

The result mcnt gives me 0. However, if I run sum(as.character(x)=="") separately, I can get empty rowcount as 2. Another thing I want to achieve is to print the first row. I call head function which doesn't work properly, either.

Upvotes: 0

Views: 263

Answers (1)

Ven Yao
Ven Yao

Reputation: 3710

I guess the problem is probably caused by R that coding characters as factors when creating data frames. See the following comparisons.

temp <- data.frame(Comment=c("fa", "fsa", "", "", "fasdf", "rew"), 
                   Prob=c(0.40768666, 0.61956024, NA, 0.12916298, 0.09724928, 0.47395962), 
                   stringsAsFactors = TRUE)

c_fun <- function(x){c(example=head(x,n=1),mcnt=sum(as.character(x)==""))}
summaryBy(Comment~., data= temp, FUN= c_fun)
#   Comment.example Comment.mcnt
# 1               2            0

temp <- data.frame(Comment=c("fa", "fsa", "", "", "fasdf", "rew"), 
                   Prob=c(0.40768666, 0.61956024, NA, 0.12916298, 0.09724928, 0.47395962), 
                   stringsAsFactors = FALSE)
summaryBy(Comment~., data= temp, FUN= c_fun)
#   Comment.example Comment.mcnt
# 1              fa            2

Upvotes: 1

Related Questions