[R] need help in if else condition
Eric Berger
er|cjberger @end|ng |rom gm@||@com
Wed Jul 10 10:38:49 CEST 2019
Change the definition of de to
de <- data.frame(d,e,stringsAsFactors=FALSE)
Then you should be ok.
Some additional remarks:
1. The ifelse() command is a bit tricky in R. Avoiding it is often a good
policy.
2. I find %in% very useful. You could replace the multiple conditions check
de$d=="-"|de$d=="dnr"|de$d=="post"
by
de$d %in% c("-","dnr","post")
Following the comments above would lead to code such as
de <- data.frame(d,e,stringsAsFactors=FALSE)
de$f <- de$d
de$f[ de$d %in% c("-","dnr","post") ] <- 0
HTH,
Eric
On Wed, Jul 10, 2019 at 10:54 AM Shubhasmita Sahani <
shubhasmita.sahani using gmail.com> wrote:
> Hello all,
> I am trying to run if else condition to alter my data. I have created a
> column F with reference to column d where I want to replace the value to
> 0 where ever it finds a string or character value like -,dnr, post and
> keep the rest of the rows should have the original value of column d. It is
> converting the text to 0 but the numeric values are not the same it is some
> value.. will you please help me to figure out why it is happening and what
> should be the proper code
>
> here are the code and the output
>
> d=c(1,2,3,"-","dnr","post",10)
> e= c(2,2,3,4,5,6,7)
> de<- as.data.frame(cbind(d,e))
> de$f<-ifelse (de$d=="-"|de$d=="dnr"|de$d=="post",0,de$d)
>
> head(de)
>
> d e f
> 1 1 2 2
> 2 2 2 4
> 3 3 3 5
> 4 - 4 0
> 5 dnr 5 0
> 6 post 6 0
> why the values are changing in with if else condition
>
>
> --
> Thanks & Regards,
> Shubhasmita Sahani
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using 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]]
More information about the R-help
mailing list