Re: [R] Recoding lists of categories of a variable

2016-10-11 Thread Bert Gunter
> Hardly a showstopper though; we're in timtowdi territory here and we're > allowed a bit of personal preference. Absolutely. I appreciate your constructive comments, however. Cheers, Bert __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and

Re: [R] Recoding lists of categories of a variable

2016-10-11 Thread S Ellison
> If you are concerned about missing levels -- which I agree is legitimate -- > then > the following simple modification works (for > **factors** of course): > > > d <- factor(letters[1:2],levels= letters[1:3]) d > [1] a b > Levels: a b c > > f <- factor(d,levels = levels(d), labels =

Re: [R] Recoding lists of categories of a variable

2016-10-11 Thread peter dalgaard
On 11 Oct 2016, at 01:32 , S Ellison wrote: >> Well, I think that's kind of overkill. > Depends whether you want to recode all or some, and how robust you want the > answer to be. > recode() allows you to recode a few levels of many, without dependence on > level

Re: [R] Recoding lists of categories of a variable

2016-10-10 Thread Bert Gunter
Still overkill, I believe. " Unlike using the numeric levels, that doesn't fail if some of the levels I expect are absent; it only fails (and does so visibly) when there's a value in there that I haven't assigned a coding to. So it's a tad more robust. " If you are concerned about missing

Re: [R] Recoding lists of categories of a variable

2016-10-10 Thread S Ellison
> Well, I think that's kind of overkill. Depends whether you want to recode all or some, and how robust you want the answer to be. recode() allows you to recode a few levels of many, without dependence on level ordering; that's kind of neat. tbh, though, I don't use recode() a lot; I

Re: [R] Recoding lists of categories of a variable

2016-10-10 Thread Jim Lemon
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

Re: [R] Recoding lists of categories of a variable

2016-10-10 Thread MACDOUGALL Margaret
-- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of > MACDOUGALL Margaret > Sent: Monday, October 10, 2016 10:56 AM > To: r-help@r-project.org > Subject: [R] Recoding lists of categories of a variable > > Hello > > The R code > mydata$newvar[oldvar =

Re: [R] Recoding lists of categories of a variable

2016-10-10 Thread Fox, John
016 10:56 AM > To: r-help@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"] <- "

Re: [R] Recoding lists of categories of a variable

2016-10-10 Thread Bert Gunter
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)) > d a 1 2 a 3 4 b 5 a 6 b 7 a 8 a 9 a 10

Re: [R] Recoding lists of categories of a variable

2016-10-10 Thread David L Carlson
8 7 7 11 topic 10topic 11topic 12topic 13topic 15topic 16 topic 17topic 18 8 10 9 13 19 12 11 3 topic 19 topic 20 18 8 --

Re: [R] Recoding lists of categories of a variable

2016-10-10 Thread S Ellison
> 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?

[R] Recoding lists of categories of a variable

2016-10-10 Thread MACDOUGALL Margaret
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