1

I have a survey object with one variable that has six factor levels. I'm trying to group the first two, the second two, and the last two levels so that I then have only three levels which I can give descriptive names to.

I have been trying to adapt:

X=update(X, Passed=factor(Pass), labels=c("No Pass", "No Pass", "Pass", "Pass", NA, NA))

But have been getting the error:

Error in `[<-.data.frame`(`*tmp*`, , newnames[j], value = c("No Pass",  : 
  replacement has 6 rows, data has 5444

Have I made a silly mistake or have I got the wrong end of the stick?

For Example:

No Pass values are 1 and 2

Pass values are 3 and 4

Then 7 and 8 are NA

Obs Passed

1         1

2         2

3         1

4         7

5         3

6         3

7         3

8         2

9         1

10        4
A.May
  • 11
  • 4
  • 1
    I think you're looking for something like `X$Pass <- factor(X$Pass); levels(X$Pass) <- c("No Pass", ...); X$Pass <- dropLevels(X$Pass)` or in one go, `X$Pass <- dropLevels(factor(X$Pass, levels = ..., labels = c("No Pass", ...)))` You should really add some data to make your example [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610), though. – alistaire Mar 28 '17 at 04:44
  • Thank you! I'll give that a go. – A.May Mar 28 '17 at 07:50
  • **Update** From the above comment, the following code worked to solve the error: `X$variables$Pass <- as.factor(X$variables$Passed); levels(X$variables$Passed) <- c("No Pass", "No Pass", "Pass", "Pass", NA, NA); X$variables$Passed <- droplevels(X$variables$Passed); summary(X$variables$Passed)` (i.e. because it is within the survey framework, the code also needed to define that "Passed" was a variable) – A.May Mar 28 '17 at 08:17

0 Answers0