coming from Java I am trying to learn R (and some statistics)
I am trying to reproduce the following table

from Jonathan Gillard: A First Course in Statistical Inference This table shows the possible results of two draws with replacement from a piggybank piggybank <- c(5, 10, 10, 20, 50, 50)
With the following code I encounter some unexpected (at least for a Java programmer) behavior.
The first seven lines basically give me what I want, except for (5, 10) and (10, 5) should be aggregated in one category. I have thought of using a set for this, but the sets library seems to mess up the first 7 lines.
library(dplyr)
rm(list=ls())
piggybank <- c(5, 10, 10, 20, 50, 50)
draws <- expand.grid(d1=piggybank, d2=piggybank)
draws <- draws %>% rowwise() %>% mutate(sum=sum(c(d1,d2)), var=var(c(d1,d2)), mean=mean(c(d1,d2)))
draws <- draws %>% group_by(d1, d2, var, mean, sum) %>% summarise(n=n())
draws <- draws %>% ungroup() %>% mutate(P=n/sum(n))
nr <- nrow(draws)
aggdraws <- data.frame(x1x2=character(0), var=numeric(0), mean=numeric(0), sum=numeric(0), n=numeric(0))
str(aggdraws)
local(
for (i in 1:nr) {
newrow <<- data.frame(x1x2=character(1), var=numeric(1), mean=numeric(1), sum=numeric(1), n=numeric(10))
newrow$n <- draws[i, ]$n
newrow$var <- draws[i, ]$var
newrow$mean <- draws[i, ]$mean
newrow$sum <- draws[i, ]$mean
newrow$x1x2 <- paste(min(draws[i, ]$d1, draws[i, ]$d2), max(draws[i, ]$d1, draws[i, ]$d2))
#print(aggdraws)
if (nrow(aggdraws) > 0) {
for(j in 1:nrow(aggdraws)) {
print(paste(aggdraws[j,]$x1x2, newrow$x1x2))
if(aggdraws[j,]$x1x2 == newrow$x1x2) {
aggdraws[j,]$n <- aggdraws[j,]$n +newrow$n
} else {
aggdraws[nrow(aggdraws)+1, ] <- newrow
}
}
} else {
aggdraws[nrow(aggdraws)+1, ] <- newrow
}
}
)
newrow <<- data.frame(x1x2=character(1), var=numeric(1), mean=numeric(1), sum=numeric(1), n=numeric(10)) creates a data.frame with 10 observations of 5 variables. Why? I need a dataframe with 1 observation.
newrow seems not to be local to the for loop, it is filled with a row in each iteration. I need a new instance in every iteration
Probably because of this behavior if(aggdraws[j,]$x1x2 == newrow$x1x2) never evaluates to TRUE
Any help would be greatly appreciated. Is there a good book or other source which points out the pitfalls of R for a programmer coming from Java or another object-oriented language?
Thanks,
Hans
newrow <<- data.frame(x1x2=character(1), var=numeric(1), mean=numeric(1), sum=numeric(1), n=numeric(10))-->n = numeric(10)creates a vector of length 10. Replace it withnumeric(1)for a vector with length 1. Probably that's the source of your strange behaviour.