Thank you all for the replies.

BWS6 is a data frame, but actually the main problem in my code as Duncan
points out is that the statement inside the parentheses modifies BWS6
anyway. I need to rework my code.

I coded my missing values for these data sets as -999.25 (a hangover from
another piece of software that dumps out results). It's a rather dangerous
thing to do, as in some cases I have discovered that when I write to csv,
-999.25 is written as -999.2 or even -999 in some cases (haven't completely
worked that out). Hence the awkward "< 998" expression.


In respect of Duncan and Peter's solutions, I have found that R can be a
troublesome pet at times, and this seems like one of them.
Thanks for the help and warnings!

Ben.


On 9 September 2013 00:39, peter dalgaard <pdalgd@gmail.com> wrote:

>
> On Sep 8, 2013, at 16:09 , Duncan Murdoch wrote:
>
> > On 13-09-08 6:46 AM, Ben Harrison wrote:
> >> Hello,
> >> I wish to create a copy of a data frame, but with missing values
> replaced
> >> with NAs.
> >>
> >> I thought I should be able to do it in one step using parentheses to
> group
> >> the statements and force those inside the parens to execute first:
> >>
> >> df <- (BWS6[BWS6 < -998] <- NA)
> >>
> >> But all this does is assign NA to df, as described in ?"[" for the case
> >> with no parens.
> >>
> >> Can I do this in some way? It's no great problem of course to have two
> >> separate statements, just curious.
> >
> > This isn't an order of execution issue.  Your parenthesized assignment
> modifies BWS6, and that's not what you want to do.
>
> Also, the value of an assignment is always the right hand side, in this
> case NA.
>
> The canonical way would be --- but There be Tygers There! --- this:
>
> df <- `[<-`(BWS6, BWS6 < -998, NA)
>
> The "Tygers" are that R sometimes cheats in order to avoid duplication and
> assumes that `[<-` can destructively modify its argument. So you shouldn't
> actually do the above.
>
> - Peter D.
>
> >
> > If you convert BWS6 to a matrix instead of a dataframe, you could use
> >
> > res <- ifelse(BWS6 < -998, NA, BWS6)
> >
> > but ifelse doesn't work on dataframes in general.  You can do it in one
> long line with a dataframe using lapply, but it is ugly:
> >
> > df <- as.data.frame( lapply(BWS6, function(col) ifelse(col < -998, NA,
> col)))
> >
> > Duncan Murdoch
> >
> > ______________________________________________
> > 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.
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Email: pd.mes@cbs.dk  Priv: PDalgd@gmail.com
>
>
>
>
>
>
>
>
>

	[[alternative HTML version deleted]]

