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".
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".
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
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