I'm confused:> x <- 1:2 > is.na(x) <- 1 > x[1] NA 2 OK, but> x <- c("A", "B") > is.na(x) <- "A" > xA "A" "B" NA What happens? G_ran
maybe this will make it clear x <- c(a=1, b=5) is.na(x) <- "b" i.e. your second case is dealing with a named vector HTH, Eric On Fri, Dec 16, 2022 at 8:29 PM G?ran Brostr?m <goran.brostrom at umu.se> wrote:> I'm confused: > > > x <- 1:2 > > is.na(x) <- 1 > > x > [1] NA 2 > > OK, but > > > x <- c("A", "B") > > is.na(x) <- "A" > > x > A > "A" "B" NA > > What happens? > > G_ran > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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]]
I don't find _either_ of these acceptable.
On the other hand,
x[ is.na( x ) ] <- 1
should have no effect on x.
On December 16, 2022 10:28:52 AM PST, "G?ran Brostr?m"
<goran.brostrom at umu.se> wrote:>I'm confused:
>
>> x <- 1:2
>> is.na(x) <- 1
>> x
>[1] NA 2
>
>OK, but
>
>> x <- c("A", "B")
>> is.na(x) <- "A"
>> x
> A
>"A" "B" NA
>
>What happens?
>
>G_ran
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.
--
Sent from my phone. Please excuse my brevity.
I think that is.na(x) <- i generally does the same to x as does x[i] <- NA I say 'generally' because some classes (e.g., numeric_version) do not allow x[i]<-NA but do allow is.na(x)<-i. It is possible that some classes mess up this equivalence, but I think that would be considered a bug. -Bill On Fri, Dec 16, 2022 at 10:29 AM G?ran Brostr?m <goran.brostrom at umu.se> wrote:> I'm confused: > > > x <- 1:2 > > is.na(x) <- 1 > > x > [1] NA 2 > > OK, but > > > x <- c("A", "B") > > is.na(x) <- "A" > > x > A > "A" "B" NA > > What happens? > > G_ran > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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]]