Hello The R code mydata$newvar[oldvar = "topic1"] <- "parenttopic" is intended to recode the category 'topic 1' of the old varaible 'oldvar' a new category label 'parenttopic' by defining the new variable 'newvar'. Is there a convenient way to edit this code to allow me to recode a list of categories 'topic 1', 'topic 9' and 'topic 14', say, of the the old variable 'oldvar' as 'parenttopic' by means of the new variable 'newvar', while also mapping system missing values to system missing values? Thanks in advance Best wishes Margaret -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20161010/4887f76b/attachment.pl>
> Is there a convenient way to edit this code to allow me to recode a list of > categories 'topic 1', 'topic 9' and 'topic 14', say, of the the old variable 'oldvar' > as 'parenttopic' by means of the new variable 'newvar', while also mapping > system missing values to system missing values?You could look at 'recode()' in the car package. There's a fair description of other options at http://www.uni-kiel.de/psychologie/rexrepos/posts/recode.html S Ellison ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
Your code suggests that you do not understand R or what you are doing. The line mydata$newvar[oldvar = "topic1"] <- "parenttopic" does not recode cases where oldvar is "topic1", it creates a new variable called oldvar (not the same as mydata$oldvar) and sets it to "topic1" because a single equals sign assigns a value to a variable whereas two equals signs create a logical expression. The result is that all values of mydata$newvar become "parenttopic". You did not give us any data, so it is not clear if oldvar is a character variable or a factor. Assuming it is a factor try the following and then spend a few hours reading some tutorials on R:> # Create reproducible data > set.seed(42) > mydata <- data.frame(oldvar=paste("topic", sample(1:20, 200, replace=TRUE))) > str(mydata)'data.frame': 200 obs. of 1 variable: $ oldvar: Factor w/ 20 levels "topic 1","topic 10",..: 11 11 17 9 5 3 7 14 6 7 ...> # Note factor levels are ordered alphabetically. Fix that with > mydata$oldvar <- factor(mydata$oldvar, levels=c(paste("topic", 1:20))) > str(mydata)'data.frame': 200 obs. of 1 variable: $ oldvar: Factor w/ 20 levels "topic 1","topic 2",..: 19 19 6 17 13 11 15 3 14 15 ...> levels(mydata$oldvar)[1] "topic 1" "topic 2" "topic 3" "topic 4" "topic 5" "topic 6" "topic 7" "topic 8" [9] "topic 9" "topic 10" "topic 11" "topic 12" "topic 13" "topic 14" "topic 15" "topic 16" [17] "topic 17" "topic 18" "topic 19" "topic 20"> mydata$newvar <- mydata$oldvar > levels(mydata$newvar)[c(1, 9, 14)] <- "parenttopic" > table(mydata$newvar)parenttopic topic 2 topic 3 topic 4 topic 5 topic 6 topic 7 topic 8 26 6 14 10 8 7 7 11 topic 10 topic 11 topic 12 topic 13 topic 15 topic 16 topic 17 topic 18 8 10 9 13 19 12 11 3 topic 19 topic 20 18 8 ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of MACDOUGALL Margaret Sent: Monday, October 10, 2016 9:56 AM To: r-help at r-project.org Subject: [R] Recoding lists of categories of a variable Hello The R code mydata$newvar[oldvar = "topic1"] <- "parenttopic" is intended to recode the category 'topic 1' of the old varaible 'oldvar' a new category label 'parenttopic' by defining the new variable 'newvar'. Is there a convenient way to edit this code to allow me to recode a list of categories 'topic 1', 'topic 9' and 'topic 14', say, of the the old variable 'oldvar' as 'parenttopic' by means of the new variable 'newvar', while also mapping system missing values to system missing values? Thanks in advance Best wishes Margaret
Well, I think that's kind of overkill. Assuming "oldvar" is a factor in the data frame mydata, then the following shows how to do it:> set.seed(27) > d <- data.frame(a = sample(c(letters[1:3],NA),15,replace = TRUE)) > da 1 <NA> 2 a 3 <NA> 4 b 5 a 6 b 7 a 8 a 9 a 10 a 11 c 12 <NA> 13 c 14 c 15 <NA>> d$b <- factor(d$a,labels = LETTERS[3:1]) > da b 1 <NA> <NA> 2 a C 3 <NA> <NA> 4 b B 5 a C 6 b B 7 a C 8 a C 9 a C 10 a C 11 c A 12 <NA> <NA> 13 c A 14 c A 15 <NA> <NA> See ?factor for details. Incidentally note that in the OP's post, mydata$newvar[oldvar = "topic1"] <- "parenttopic" is completely incorrect; it should probably be: mydata$newvar[mydata$oldvar == "topic1"] <- "parenttopic"; This suggests to me that the OP would probably find it useful to spend some time with one or more of the many good R tutorials on the web. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Oct 10, 2016 at 9:08 AM, S Ellison <S.Ellison at lgcgroup.com> wrote:>> Is there a convenient way to edit this code to allow me to recode a list of >> categories 'topic 1', 'topic 9' and 'topic 14', say, of the the old variable 'oldvar' >> as 'parenttopic' by means of the new variable 'newvar', while also mapping >> system missing values to system missing values? > > You could look at 'recode()' in the car package. > > There's a fair description of other options at http://www.uni-kiel.de/psychologie/rexrepos/posts/recode.html > > S Ellison > > > > > ******************************************************************* > This email and any attachments are confidential. Any u...{{dropped:8}}
Dear Margaret, You've had one suggestion of an alternative for recoding variables, but in addition your code is in error (see below).> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of > MACDOUGALL Margaret > Sent: Monday, October 10, 2016 10:56 AM > To: r-help at r-project.org > Subject: [R] Recoding lists of categories of a variable > > Hello > > The R code > mydata$newvar[oldvar = "topic1"] <- "parenttopic"That should be mydata$newvar[oldvar == "topic1"] <- "parenttopic" Moreover, the code assumes that oldvar is visible, which may not be the case if it lives in mydata and mydata isn't attach()ed. Best, John -------------------------------------- John Fox, Professor McMaster University Hamilton, Ontario, Canada Web: socserv.mcmaster.ca/jfox> > is intended to recode the category 'topic 1' of the old varaible > 'oldvar' a new category label 'parenttopic' by defining the new variable > 'newvar'. > > Is there a convenient way to edit this code to allow me to recode a list > of categories 'topic 1', 'topic 9' and 'topic 14', say, of the the old > variable 'oldvar' as 'parenttopic' by means of the new variable > 'newvar', while also mapping system missing values to system missing > values? > > Thanks in advance > > Best wishes > Margaret
Thank you for the valued suggestions in response to my query. Margaret -- The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336. -----Original Message----- From: Fox, John [mailto:jfox at mcmaster.ca] Sent: 10 October 2016 20:32 To: MACDOUGALL Margaret <Margaret.MacDougall at ed.ac.uk> Cc: r-help at r-project.org Subject: RE: Recoding lists of categories of a variable Dear Margaret, You've had one suggestion of an alternative for recoding variables, but in addition your code is in error (see below).> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of > MACDOUGALL Margaret > Sent: Monday, October 10, 2016 10:56 AM > To: r-help at r-project.org > Subject: [R] Recoding lists of categories of a variable > > Hello > > The R code > mydata$newvar[oldvar = "topic1"] <- "parenttopic"That should be mydata$newvar[oldvar == "topic1"] <- "parenttopic" Moreover, the code assumes that oldvar is visible, which may not be the case if it lives in mydata and mydata isn't attach()ed. Best, John -------------------------------------- John Fox, Professor McMaster University Hamilton, Ontario, Canada Web: socserv.mcmaster.ca/jfox> > is intended to recode the category 'topic 1' of the old varaible > 'oldvar' a new category label 'parenttopic' by defining the new > variable 'newvar'. > > Is there a convenient way to edit this code to allow me to recode a > list of categories 'topic 1', 'topic 9' and 'topic 14', say, of the > the old variable 'oldvar' as 'parenttopic' by means of the new > variable 'newvar', while also mapping system missing values to system > missing values? > > Thanks in advance > > Best wishes > Margaret
Hi Margaret, This may be a misunderstanding of your request, but what about: mydata<-data.frame(oldvar=paste("topic",sample(1:9,20,TRUE),sep="")) mydata$newvar<-sapply(mydata$oldvar,gsub,"topic.","parenttopic") Jim On Tue, Oct 11, 2016 at 1:56 AM, MACDOUGALL Margaret <Margaret.MacDougall at ed.ac.uk> wrote:> Hello > > The R code > mydata$newvar[oldvar = "topic1"] <- "parenttopic" > > is intended to recode the category 'topic 1' of the old varaible 'oldvar' a new category label 'parenttopic' by defining the new variable 'newvar'. > > Is there a convenient way to edit this code to allow me to recode a list of categories 'topic 1', 'topic 9' and 'topic 14', say, of the the old variable 'oldvar' as 'parenttopic' by means of the new variable 'newvar', while also mapping system missing values to system missing values? > > Thanks in advance > > Best wishes > Margaret > > > The University of Edinburgh is a charitable body, registered in > Scotland, with registration number SC005336. > > ______________________________________________ > 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.