Skip to content
Back to formatted view

Raw Message

Message-ID: <CAM_vjuk01hQ32swZLPAXkLtMbNPqHBUXrWXcPfYn7_PXXR=uwg@mail.gmail.com>
Date: 2011-10-18T18:24:56Z
From: Sarah Goslee
Subject: Detect and replace omitted data
In-Reply-To: <CAAXorZNqRNEbAPyEXPSCEMtp0_CgEgfn3eKj5hA+Zmzwdy0Aww@mail.gmail.com>

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