error in IF condition with factor evaluation

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

error in IF condition with factor evaluation

edoardo baldoni
Cam anyone tell me why the condition x[i] == "DISCONECTED" looks like
producing an NA instead of TRUE/FALSE

I would like to rename "DISCONNECTED" those factors inside the variable
"dataset$STATUS.x" that are named "DISCONECTED"

thank you


> summary(dataset$STATUS.x)
     ACTIVE DISCONECTED     PENDING   SUSPENDED  TERMINATED
     158869      169181        3028        8565       47233
       NA's
          6
> class(dataset$STATUS.x)
[1] "factor"
>
> fff = function(x) {
+ for (i in 1:length(x)){
+ if (x[i] == "DISCONECTED") {
+ x[i] == "DISCONNECTED"
+ } else x[i] == x[i]
+ }
+ return(x)
+ }
>
> r = fff(dataset$STATUS.x)
Error in if (x[i] == "DISCONECTED") { :
  missing value where TRUE/FALSE needed

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: error in IF condition with factor evaluation

Eik Vettorazzi-2
Hi Edoardo,
there is a difference between comparisons and assignments, both
semantically as well as in R syntax: "==" vs "=" or "<-", latter being
more obvious an assignment.
This is the source of your error.
But to change the labels of a factor object, it is easier to do sth like

at<-factor(1:5,labels=letters[1:5])
at
levels(at)[3]<-"xyz"
at   #check

hth



Am 23.11.2012 10:42, schrieb edoardo baldoni:

> Cam anyone tell me why the condition x[i] == "DISCONECTED" looks like
> producing an NA instead of TRUE/FALSE
>
> I would like to rename "DISCONNECTED" those factors inside the variable
> "dataset$STATUS.x" that are named "DISCONECTED"
>
> thank you
>
>
>> summary(dataset$STATUS.x)
>      ACTIVE DISCONECTED     PENDING   SUSPENDED  TERMINATED
>      158869      169181        3028        8565       47233
>        NA's
>           6
>> class(dataset$STATUS.x)
> [1] "factor"
>>
>> fff = function(x) {
> + for (i in 1:length(x)){
> + if (x[i] == "DISCONECTED") {
> + x[i] == "DISCONNECTED"
> + } else x[i] == x[i]
> + }
> + return(x)
> + }
>>
>> r = fff(dataset$STATUS.x)
> Error in if (x[i] == "DISCONECTED") { :
>   missing value where TRUE/FALSE needed
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>


--
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790
--
Pflichtangaben gemäß Gesetz über elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG):

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; Gerichtsstand: Hamburg

Vorstandsmitglieder: Prof. Dr. Martin Zeitz (Vorsitzender), Dr. Alexander Kirstein, Joachim Prölß, Prof. Dr. Dr. Uwe Koch-Gromus

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: error in IF condition with factor evaluation

Rui Barradas
In reply to this post by edoardo baldoni
Hello,

Try

if (!is.na(x[i]) && x[i] == "DISCONECTED")


Hope this helps,

Rui Barradas
Em 23-11-2012 09:42, edoardo baldoni escreveu:

> Cam anyone tell me why the condition x[i] == "DISCONECTED" looks like
> producing an NA instead of TRUE/FALSE
>
> I would like to rename "DISCONNECTED" those factors inside the variable
> "dataset$STATUS.x" that are named "DISCONECTED"
>
> thank you
>
>
>> summary(dataset$STATUS.x)
>       ACTIVE DISCONECTED     PENDING   SUSPENDED  TERMINATED
>       158869      169181        3028        8565       47233
>         NA's
>            6
>> class(dataset$STATUS.x)
> [1] "factor"
>> fff = function(x) {
> + for (i in 1:length(x)){
> + if (x[i] == "DISCONECTED") {
> + x[i] == "DISCONNECTED"
> + } else x[i] == x[i]
> + }
> + return(x)
> + }
>> r = fff(dataset$STATUS.x)
> Error in if (x[i] == "DISCONECTED") { :
>    missing value where TRUE/FALSE needed
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: error in IF condition with factor evaluation

PIKAL Petr
Hi

> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Rui Barradas
> Sent: Friday, November 23, 2012 2:37 PM
> To: edoardo baldoni
> Cc: [hidden email]
> Subject: Re: [R] error in IF condition with factor evaluation
>
> Hello,
>
> Try
>
> if (!is.na(x[i]) && x[i] == "DISCONECTED")
>
>
> Hope this helps,
>
> Rui Barradas
> Em 23-11-2012 09:42, edoardo baldoni escreveu:
> > Cam anyone tell me why the condition x[i] == "DISCONECTED" looks like
> > producing an NA instead of TRUE/FALSE
> >
> > I would like to rename "DISCONNECTED" those factors inside the
> > variable "dataset$STATUS.x" that are named "DISCONECTED"


If
dataset$STATUS.x

is factor, you can use

levels(dataset$STATUS.x)[2] <-"DISCONNECTED"

to rename second level (which appears to be DISCONECTED)

Regaards

Petr

> >
> > thank you
> >
> >
> >> summary(dataset$STATUS.x)
> >       ACTIVE DISCONECTED     PENDING   SUSPENDED  TERMINATED
> >       158869      169181        3028        8565       47233
> >         NA's
> >            6
> >> class(dataset$STATUS.x)
> > [1] "factor"
> >> fff = function(x) {
> > + for (i in 1:length(x)){
> > + if (x[i] == "DISCONECTED") {
> > + x[i] == "DISCONNECTED"
> > + } else x[i] == x[i]
> > + }
> > + return(x)
> > + }
> >> r = fff(dataset$STATUS.x)
> > Error in if (x[i] == "DISCONECTED") { :
> >    missing value where TRUE/FALSE needed
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.