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-boun...@r-project.org] On Behalf Of MACDOUGALL 
Margaret
Sent: Monday, October 10, 2016 9: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"

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

______________________________________________
R-help@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.

Reply via email to