# NOT RUN {
# all need expansion
( d1 <- data.frame(name=c("Johnson","Johnson","Jones","Frank","Frank","Max"),
lwr.bin=c(15,15.5,16,16,17,17),
upr.bin=c(15.5,16,16.5,16.5,17.5,17.5),
freq=c(6,4,2,3,1,1)) )
expandCounts(d1,~freq)
expandCounts(d1,~freq,~lwr.bin+upr.bin)
# some need expansion
( d2 <- data.frame(name=c("Johnson","Johnson","Jones","Frank","Frank","Max"),
lwr.bin=c(15,15.5,16,16,17.1,17.3),
upr.bin=c(15.5,16,16.5,16.5,17.1,17.3),
freq=c(6,4,2,3,1,1)) )
expandCounts(d2,~freq)
expandCounts(d2,~freq,~lwr.bin+upr.bin)
# none need expansion
( d3 <- data.frame(name=c("Johnson","Johnson","Jones","Frank","Frank","Max"),
lwr.bin=c(15,15.5,16,16,17.1,17.3),
upr.bin=c(15,15.5,16,16,17.1,17.3),
freq=c(6,4,2,3,1,1)) )
expandCounts(d3,~freq)
expandCounts(d3,~freq,~lwr.bin+upr.bin)
# some need expansion, but different bin widths
( d4 <- data.frame(name=c("Johnson","Johnson","Jones","Frank","Frank","Max"),
lwr.bin=c(15, 15, 16, 16, 17.1,17.3),
upr.bin=c(15.5,15.9,16.5,16.9,17.1,17.3),
freq=c(6,4,2,3,1,1)) )
expandCounts(d4,~freq)
expandCounts(d4,~freq,~lwr.bin+upr.bin)
# some need expansion but include zeros and NAs for counts
( d2a <- data.frame(name=c("Johnson","Johnson","Jones","Frank","Frank","Max","Max","Max","Max"),
lwr.bin=c(15, 15.5,16 ,16 ,17.1,17.3,NA,NA,NA),
upr.bin=c(15.5,16 ,16.5,16.5,17.1,17.3,NA,NA,NA),
freq=c(6,4,2,3,1,1,NA,0,NA)) )
expandCounts(d2a,~freq,~lwr.bin+upr.bin)
# some need expansion but include NAs for upper values
( d2b <- data.frame(name=c("Johnson","Johnson","Jones","Frank","Frank","Max"),
lwr.bin=c(15, 15.5,16 ,16 ,17.1,17.3),
upr.bin=c(NA ,NA ,16.5,16.5,17.1,17.3),
freq=c(6,4,2,3,1,1)) )
expandCounts(d2b,~freq,~lwr.bin+upr.bin)
# some need expansion but include NAs for upper values
( d2c <- data.frame(name=c("Johnson","Johnson","Jones","Frank","Frank","Max"),
lwr.bin=c(NA,NA, 16 ,16 ,17.1,17.3),
upr.bin=c(15,15.5,16.5,16.5,17.1,17.3),
freq=c(6,4,2,3,1,1)) )
expandCounts(d2c,~freq,~lwr.bin+upr.bin)
# }
# NOT RUN {
##!!##!!## Change path to where example file is and then run to demo
## Read in datafile (note periods in names)
df <- read.csv("c:/aaawork/consulting/R_WiDNR/Statewide/Surveysummaries2010.csv")
str(df)
## narrow variables for simplicity
df1 <- df[,c("County","Waterbody.Name","Survey.Year","Gear","Species","Number.of.Fish",
"Length.or.Lower.Length.IN","Length.Upper.IN","Weight.Pounds","Gender")]
## Sum the count to see how many fish there should be after expansion
sum(df1$Number.of.Fish)
## Simple expansion
df2 <- expandCounts(df1,~Number.of.Fish)
## Same expansion but include random component to lengths (thus new length variable)
## also note default lprec=0.1
df3 <- expandCounts(df1,~Number.of.Fish,~Length.or.Lower.Length.IN+Length.Upper.IN)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab