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 mo

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 = LETTERS[3:1

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 ordering; that's kind of neat

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 level

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 generall

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 wrote: > Hello > > The R c

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

2016-10-10 Thread MACDOUGALL 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:j...@mcmaster.ca] Sent: 10 October 2016 20:32 To: MACDOUGA

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

2016-10-10 Thread Fox, John
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-boun...@r-project.org] On Behalf Of > MACDOUGALL Margaret > Sent: Monday, October 10, 2016 10:56 AM > T

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
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 equa

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? Yo