Hi all, I have a very general question and I think for you maybe very easy, but I am not able to solve it. I have a dataset and that dataset contains the variable Anxiolytics. This variable is coded as 0, 1, 2. The variable looks as follows:> summary(DF$Anxiolytics)0 1 2 NA's 1102 0 20 440 You can see that the variable is coded as 0, 1, 2, but group 1 is 'empty'. So I want to remove this group, such that I get:> summary(DF$Anxiolytics)0 2 NA's 1102 20 440 And then I want to recode the variable in a way that 2 becomes 1, such as:> summary(DF$Anxiolytics)0 1 NA's 1102 20 440 Can you help me with the code for doing this? Thank you in advance! Best, Lisa [[alternative HTML version deleted]]
It looks like your data has class "factor". If you call factor() on a factor variable, without supplying an explicit 'levels' argument it produces a new factor variable without any levels not present in the input factor. E.g.,> fOrig <- factor(c(NA,"A","B","D",NA,"D"), levels=c("D","C","B","A")) > summary(fOrig)D C B A NA's 2 0 1 1 2> fNew <- factor(fOrig) > summary(fNew)D B A NA's 2 1 1 2 You can use levels(fNew) <- c("delta","beta","alpha") to give the levels new names. I think that renaming "0", "2", "3" to "0","1","2" will lead to confusion. The levels are just names, so give them names that mean something to a human, like "control", "streptomycin (2 mg/kg)", etc. Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, May 23, 2018 at 3:14 AM, Lisa van der Burgh <lisavdburgh at hotmail.com> wrote:> Hi all, > > > I have a very general question and I think for you maybe very easy, but I > am not able to solve it. > > I have a dataset and that dataset contains the variable Anxiolytics. This > variable is coded as 0, 1, 2. The variable looks as follows: > > > > summary(DF$Anxiolytics) > 0 1 2 NA's > 1102 0 20 440 > > > You can see that the variable is coded as 0, 1, 2, but group 1 is 'empty'. > So I want to remove this group, such that I get: > > > summary(DF$Anxiolytics) > 0 2 NA's > 1102 20 440 > > And then I want to recode the variable in a way that 2 becomes 1, such as: > > > summary(DF$Anxiolytics) > 0 1 NA's > 1102 20 440 > > Can you help me with the code for doing this? > > Thank you in advance! > Best, Lisa > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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]]
Hello, See if this inspires you. set.seed(1962) DF <- data.frame(Anxiolytics = factor(sample(c(0, 2, NA), 1000, TRUE), levels = 0:2)) summary(DF$Anxiolytics) DF$Anxiolytics <- droplevels(DF$Anxiolytics) summary(DF$Anxiolytics) DF$Anxiolytics <- factor(DF$Anxiolytics, labels = 0:1) summary(DF$Anxiolytics) Hope this helps, Rui Barradas On 5/23/2018 11:14 AM, Lisa van der Burgh wrote:> Hi all, > > > I have a very general question and I think for you maybe very easy, but I am not able to solve it. > > I have a dataset and that dataset contains the variable Anxiolytics. This variable is coded as 0, 1, 2. The variable looks as follows: > > >> summary(DF$Anxiolytics) > 0 1 2 NA's > 1102 0 20 440 > > > You can see that the variable is coded as 0, 1, 2, but group 1 is 'empty'. So I want to remove this group, such that I get: > >> summary(DF$Anxiolytics) > 0 2 NA's > 1102 20 440 > > And then I want to recode the variable in a way that 2 becomes 1, such as: > >> summary(DF$Anxiolytics) > 0 1 NA's > 1102 20 440 > > Can you help me with the code for doing this? > > Thank you in advance! > Best, Lisa > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >