Ottar Kvindesland wrote:> Hi,
>
> I am collecting replies from a survey and counts replies by the table()
> function. The function below carries two
> data frames and counts the observations of the findings in the first
> parameter vector given the value of the second as shown in the code below.
>
> My trouble is that the vector kp_vec at the end of the inner loop seems to
> ignore the value when the
> table() counts zero occurences of one of the outcomes. I will have
>
> y n
> 34 0
>
> This is not picked up in the matrix row after the loops with something like
>
> "y" "n"
"y"
> "Funding" "Survival" 12 5 34
>
> where the last "n" value is missing. This causes my returned data
frame to
> fail and in all, rather miserable for the plot.
>
> I see the point of this in a way, so I believe it is not a bug. I'd
love to
> get my zero back. Is it a subtle point in R I have missed?
I don't know about subtle, but the general idea is that if you can only
expect to get a zero count if the set of possible values is known in
advance to include that value. Otherwise there's just no end to the
number of zeros to include! This can be obtained by making the object
you tabulate into a factor with the relevant level set:
> table("y")
y
1> table(factor("y",levels=c("y","n")))
y n
1 0
Incidentally, this is the main reason R doesn't by default drop unused
levels when subsetting.
I can't make heads or tails of your code, but I hope the above helps
solve your issue.
-pd
>
>
> kpi_test <- function ( paramvec, kpivec ) {
> kp_vec <- c()
> res_kpi_y <- c()
> res_kpi_n <- c()
>
> tmp_param <- c()
> tmp_kpi <- c()
>
> for(param_no in seq(from=1, to=length(paramvec), by = 1)) {
> tmp_param <- paramvec[param_no]
> for (kpi_no in seq(from=1, to=length(kpivec), by = 1)) {
> tmp_kpi <- kpivec[kpi_no]
> res_kpi_y <- table( tmp_param [ tmp_kpi == 'y' ] )
> res_kpi_n <- table( tmp_param [ tmp_kpi == 'n' ] )
> kp_vec <- c(kp_vec, names(tmp_param), names(tmp_kpi), res_kpi_y,
res_kpi_n )
> }
> }
> matrix_vector <- matrix(kp_vec, ncol=6, byrow=T)
> fres <- data.frame(matrix_vector)
> return( fres )
> }
>
>
> ottar
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Phone: (+45)38153501
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com