You don't need to use 'replace', just use indexing in the
assignment:
isCrap <- apply((wheat2[,5:7] > wheat2[,8]), 1, any)
wheat2[isCrap,5:7] <- NA
Felix
On 7/25/07, willem vervoort <willemvervoort at gmail.com>
wrote:> Hi
>
> I think I have been struggling to use replace correctly, I usually
> work my way around this using a loop, but I think this is in fact
> inefficient.
>
> I have a dataset with runoff from three plots and associated rainfall.
> However either the datarecording was sloppy, or the rainfall very
> patchy. So I am trying to remove data from my dataset for which the
> runoff is larger than the rainfall on the same day. (Don't worry, the
> rainfall data is in fact the rainfall associated to the three days
> around the runoff event). On days that no runoff was recorded the data
> includes "NA"
>
> The dataset is called wheat2
> I use the following:
> for (i in 1:nrow(wheat2)) {
> # Insert NA's if runoff data > rainfall data
> if (is.na(wheat2[i,5:7])==FALSE &&
any(wheat2[i,5:7]>wheat2[i,8])) {
> wheat2[i,5:7] <- NA
> }
> }
>
> I tried this:
> wheat1 <- replace(wheat2[,5:7],is.na(wheat2[i,5:7])==FALSE &&
> any(wheat2[i,5:7]>wheat2[i,8]),NA)
> wheat3 <- cbind(wheat2[1:4],wheat1,wheat2[,8])
>
> > wheat3[5539,] # the culprit row
> DateRain Rain StartDate EndDate RO.A RO.B RO.C filtered.Rain
> 5539 28-Feb-58 0 27-Feb-58 28-Feb-58 61.5 88.7
> 65 0
>
> Not sure what I am doing wrong, any help is appreciated
>
> Willem
>
> Here is a data sample and note the very high value for runoff (RO) and
> no rainfall on 28 Feb 1958
>
> DateRain Rain StartDate EndDate RO.A RO.B RO.C filtered.Rain
> 14-Feb-58 0 NA NA NA 0
> 15-Feb-58 0 NA NA NA 0
> 16-Feb-58 0 NA NA NA 0
> 17-Feb-58 0 NA NA NA 0
> 18-Feb-58 0 18-Feb-58 18-Feb-58 0 0 0 23.6
> 19-Feb-58 23.6 NA NA NA 23.6
> 20-Feb-58 0 NA NA NA 23.6
> 21-Feb-58 0 NA NA NA 0
> 22-Feb-58 0 NA NA NA 0
> 23-Feb-58 0 NA NA NA 0
> 24-Feb-58 0 NA NA NA 0
> 25-Feb-58 0 NA NA NA 0
> 26-Feb-58 0 NA NA NA 0
> 27-Feb-58 0 NA NA NA 0
> 28-Feb-58 0 27-Feb-58 28-Feb-58 61.5 88.7 65 0
> 01-Mar-58 0 NA NA NA 0
> 02-Mar-58 0 NA NA NA 0
> 03-Mar-58 0 NA NA NA 0
> 04-Mar-58 0 NA NA NA 1.5
> 05-Mar-58 1.5 NA NA NA 1.5
> 06-Mar-58 0 NA NA NA 1.5
> 07-Mar-58 0 NA NA NA 0.5
> 08-Mar-58 0.5 NA NA NA 0.5
> 09-Mar-58 0 NA NA NA 7.6
> 10-Mar-58 7.1 NA NA NA 9.1
> 11-Mar-58 2 NA NA NA 57.4
> 12-Mar-58 48.3 09-Mar-58 12-Mar-58 0.1 1.5 0 51.1
> 13-Mar-58 0.8 NA NA NA 49.9
> 14-Mar-58 0.8 NA NA NA 1.6
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
--
Felix Andrews / ???
PhD candidate
Integrated Catchment Assessment and Management Centre
The Fenner School of Environment and Society
The Australian National University (Building 48A), ACT 0200
Beijing Bag, Locked Bag 40, Kingston ACT 2604
http://www.neurofractal.org/felix/
voice:+86_1051404394 (in China)
mobile:+86_13522529265 (in China)
mobile:+61_410400963 (in Australia)
xmpp:foolish.android at gmail.com
3358 543D AAC6 22C2 D336 80D9 360B 72DD 3E4C F5D8