I have a factor column, with three values: "b", "c" and "free".
I did
df$new_col = as.numeric (df$factor_col)
But it will convert "b" to 1, "c" to 2 and "free" to 3.
But I want to convert "free" to 0, "b" to 2 and "c" to 5. How can I do it in R?
Thanks a lot
f <- factor(c("b", "c", "c", "free", "b", "free"))
You can try renaming the factor levels,
levels(f)[levels(f)=="b"] <- 2
levels(f)[levels(f)=="c"] <- 5
levels(f)[levels(f)=="free"] <- 0
> f
#[1] 2 5 5 0 2 0
#Levels: 2 5 0
One option would be to call the 'factor' again and specify the levels
and labels
argument based on the custom order and change to numeric
after converting to 'character' or through the levels
df$new_col <- as.numeric(as.character(factor(df$factor_col,
levels=c('b', 'c', 'free'), labels=c(2, 5, 0))))
Another option is recode
from library(car)
. The output will be factor
class. If we need to convert to 'numeric', we can do this as in the earlier solution ( as.numeric(..
).
library(car)
df$new_col <- with(df, recode(factor_col, "'b'=2; 'c'=5; 'free'=0"))
df <- data.frame(factor_col= c('b', 'c', 'b', 'free', 'c', 'free'))
You can use the following approach to create the new column:
# an example data frame
f <- data.frame(factor_col = c("b", "c", "free"))
# create new_col
f <- transform(f, new_col = (factor_col == "b") * 2 + (factor_col == "c") * 5)
The result ( f
):
factor_col new_col
1 b 2
2 c 5
3 free 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.