I am analyzing the spatial distribution of fish in a stream. The stream is divided into equally sized units, and the number of fish in each unit is counted. My problem is that my dataset is missing rows where the count in a unit equals zero. I need to create zero data for the missing units. For example: day<-(c(rep(4,8),rep(6,8))) unit<-c(seq(1,8,1),seq(2,16,2)) value<-floor(rnorm(16,25,10)) x<-cbind(day,unit,value) x day unit value [1,] 4 1 19 [2,] 4 2 15 [3,] 4 3 16 [4,] 4 4 20 [5,] 4 5 17 [6,] 4 6 15 [7,] 4 7 14 [8,] 4 8 29 [9,] 6 2 18 [10,] 6 4 22 [11,] 6 6 27 [12,] 6 8 16 [13,] 6 10 45 [14,] 6 12 36 [15,] 6 14 34 [16,] 6 16 13 Lets say the stream has 16 units. For each day, I want to fill in rows for any missing units (e.g., units 9-16 for day 4, the odd numbered units on day 6) with values of zero. Does anyone know a relatively concise way to do this? Thank you. [[alternative HTML version deleted]]
Hi Jonny, On Tue, Oct 18, 2011 at 1:02 PM, Jonny Armstrong <jonny5armstrong at gmail.com> wrote:> I am analyzing the spatial distribution of fish in a stream. The stream is > divided into equally sized units, and the number of fish in each unit is > counted. My problem is that my dataset is missing rows where the count in a > unit equals zero. I need to create zero data for the missing units. > > For example: > day<-(c(rep(4,8),rep(6,8))) > unit<-c(seq(1,8,1),seq(2,16,2)) > value<-floor(rnorm(16,25,10)) > x<-cbind(day,unit,value)Thanks for the actual reproducible example.> x > ? ? ?day unit value > ?[1,] ? 4 ? ?1 ? ?19 > ?[2,] ? 4 ? ?2 ? ?15 > ?[3,] ? 4 ? ?3 ? ?16 > ?[4,] ? 4 ? ?4 ? ?20 > ?[5,] ? 4 ? ?5 ? ?17 > ?[6,] ? 4 ? ?6 ? ?15 > ?[7,] ? 4 ? ?7 ? ?14 > ?[8,] ? 4 ? ?8 ? ?29 > ?[9,] ? 6 ? ?2 ? ?18 > [10,] ? 6 ? ?4 ? ?22 > [11,] ? 6 ? ?6 ? ?27 > [12,] ? 6 ? ?8 ? ?16 > [13,] ? 6 ? 10 ? ?45 > [14,] ? 6 ? 12 ? ?36 > [15,] ? 6 ? 14 ? ?34 > [16,] ? 6 ? 16 ? ?13 > > Lets say the stream has 16 units. For each day, I want to fill in rows for > any missing units (e.g., units 9-16 for day 4, the odd numbered units on day > 6) with values of zero.Here's one option, though it may not be terribly concise: all.samples <- expand.grid(day=unique(x[,"day"]), unit=1:16) all.samples <- all.samples[order(all.samples[,"day"], all.samples[,"unit"]),] x.final <- merge(x, all.samples, all.y=TRUE) x.final[is.na(x.final[,"value"]), "value"] <- 0 Sarah> Does anyone know a relatively concise way to do this? > Thank you. > > ? ? ? ?[[alternative HTML version deleted]] >-- Sarah Goslee http://www.functionaldiversity.org
Here is one option: a<- data.frame(day=c(rep(4,8),rep(6,8)),unitc((1:8),seq(2,16,2)),value=round(runif(16,1,34),0)) #approx your data b<- data.frame(day=c(rep(4,16),rep(6,16)),unit= 1:16) #fake df b1<-merge (a,b, by=c('day','unit'),all.y=T) b1$value[is.na(b1$value)]<-0 ---------------------------- On Tue, Oct 18, 2011 at 10:02 AM, Jonny Armstrong <jonny5armstrong@gmail.com> wrote:> I am analyzing the spatial distribution of fish in a stream. The stream is > divided into equally sized units, and the number of fish in each unit is > counted. My problem is that my dataset is missing rows where the count in a > unit equals zero. I need to create zero data for the missing units. > > For example: > day<-(c(rep(4,8),rep(6,8))) > unit<-c(seq(1,8,1),seq(2,16,2)) > value<-floor(rnorm(16,25,10)) > x<-cbind(day,unit,value) > > x > day unit value > [1,] 4 1 19 > [2,] 4 2 15 > [3,] 4 3 16 > [4,] 4 4 20 > [5,] 4 5 17 > [6,] 4 6 15 > [7,] 4 7 14 > [8,] 4 8 29 > [9,] 6 2 18 > [10,] 6 4 22 > [11,] 6 6 27 > [12,] 6 8 16 > [13,] 6 10 45 > [14,] 6 12 36 > [15,] 6 14 34 > [16,] 6 16 13 > > Lets say the stream has 16 units. For each day, I want to fill in rows for > any missing units (e.g., units 9-16 for day 4, the odd numbered units on > day > 6) with values of zero. > > Does anyone know a relatively concise way to do this? > Thank you. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]