Ein eingebundener Text mit undefiniertem Zeichensatz wurde abgetrennt. Name: nicht verf?gbar URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120412/74331e9a/attachment.pl>
Hi David,
You bring up a good question. I am not sure what is the "right" way
to
solve it. But here is a simple solution I put together:
x = c(1:10,5)
y = x
x[c(2,3)] <- NA
# reproducing the problem:
y[x==5]
na2F <- function(x) {
x2 <- x
x2[is.na(x)] <- F
x2
}
na2F(x==5)
# "solved"
y[na2F(x==5)]
I'd be happy to see other solutions to it.
With regards,
Tal
----------------Contact
Details:-------------------------------------------------------
Contact me: Tal.Galili@gmail.com | 972-52-7275845
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
www.r-statistics.com (English)
----------------------------------------------------------------------------------------------
On Thu, Apr 12, 2012 at 12:08 PM, David Studer <studerov@gmail.com> wrote:
> Hello everybody,
>
> I know this is pretty basic stuff, but could anyone explain me how to
> recode a single value of a variable
> into a missing value?
>
> I used to do it like this:
>
> myData[myData$var1==5;"var1"]<-NA # recode value
"5" into "NA"
>
> But the column "var1" already contains NAs, which
> results in the following error message:
>
> "missing values are not allowed in subscripted assignments of data
frames"
>
> Thank you very much for any advice!
>
> David
>
> [[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]]
Le jeudi 12 avril 2012 ? 11:08 +0200, David Studer a ?crit :> Hello everybody, > > I know this is pretty basic stuff, but could anyone explain me how to > recode a single value of a variable > into a missing value? > > I used to do it like this: > > myData[myData$var1==5;"var1"]<-NA # recode value "5" into "NA" > > But the column "var1" already contains NAs, which > results in the following error message: > > "missing values are not allowed in subscripted assignments of data frames" > > Thank you very much for any advice!You can just do this: myData <- data.frame(var1=1:10) myData$var1[2]<-NA myData[myData$var1 == 5, "var1"] <- NA # Fails myData$var1[myData$var1 == 5] <- NA # Works myData var1 1 1 2 NA 3 3 4 4 5 NA 6 6 7 7 8 8 9 9 10 10 Regards
1. Some data structured the way you are using would have been helpful.
I used Tal Galil's play data and set up a dataframe with the variable names
you are using:
structure(list(var1 = c(1, NA, NA, 4, 5, 6, 7, 8, 9, 10, 5),
var2 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5)), .Names = c("var1",
"var2"), row.names = c(NA, -11L), class =
"data.frame")> myData
var1 var2
1 1 1
2 NA 2
3 NA 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
10 10 10
11 5 5
2. The error message I get from your line of code is> myData[myData$var1==5;"var1"]<-NA
Error: unexpected ';' in "myData[myData$var1==5;"
Probably the semikolon is a typo?
3. If I understand your question correctly, the easiest answer I can find is
ifelse():> myData$var1 <- ifelse( myData$var1 == 5, NA, myData$var1 )
> myData
var1 var2
1 1 1
2 NA 2
3 NA 3
4 4 4
5 NA 5
6 6 6
7 7 7
8 8 8
9 9 9
10 10 10
11 NA 5
Rgds,
Rainer
On Thursday 12 April 2012 11:08:45 David Studer wrote:> Hello everybody,
>
> I know this is pretty basic stuff, but could anyone explain me how to
> recode a single value of a variable
> into a missing value?
>
> I used to do it like this:
>
> myData[myData$var1==5;"var1"]<-NA # recode value
"5" into "NA"
>
> But the column "var1" already contains NAs, which
> results in the following error message:
>
> "missing values are not allowed in subscripted assignments of data
frames"
>
> Thank you very much for any advice!
>
> David
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
> myData[myData$var1==5;"var1"]<-NA # recode value "5" into "NA"try myData[!is.na(myData$var1) & myData$var1==5;"var1"]<-NA or, more simply, myData$var1[myData$var1==5]<-NA ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}