0

My data frame has 3 factor variables and their values are:

"It was less than adequate for household needs", 
"It was just adequate for household needs", 
"It was more than adequate for household needs"

and I need them to be "1", "2", and "3".

M--
  • 25,431
  • 8
  • 61
  • 93
natalia
  • 39
  • 1
  • 2
  • 3
  • Also take a look at https://stackoverflow.com/q/29711067/5325862, https://stackoverflow.com/q/38621334/5325862, https://stackoverflow.com/q/28190435/5325862 – camille Jan 19 '20 at 19:53

2 Answers2

0

You can simply do:

df <- data.frame(col=c("It was less than adequate for household needs", 
                       "It was just adequate for household needs",
                       "It was more than adequate for household needs"))

# suggested by @akrun (maintains the order)
df$col <- as.integer(factor(df$col, levels = unique(df$col)))

  col
1   1
2   2
3   3

If you want to maintain a sequential order, may be just add a new column with sequences:

df$code <- seq(nrow(df))

                                            col code
1 It was less than adequate for household needs    1
2      It was just adequate for household needs    2
3 It was more than adequate for household needs    3
YOLO
  • 20,181
  • 5
  • 20
  • 40
-2

Using the data frame from @YOLO's answer:

> df$col <- factor(df$col, levels = levels(df$col), labels = 1:3)
> df
  col
1   2
2   1
3   3
BroVic
  • 979
  • 9
  • 26