I have the following dataframe:
df_raw <- cbind( P1=c(1,1,2,2,3), P2=c(2,3,4,4,4) )
How can I convert these numbers to factors with different levels, so that I have something like:
head(df_factor)
P1 P2
1 "alpha" "beta"
2 "alpha" "gamma"
3 "beta" "delta"
i.e. where 1 gets converted to "alpha", 2 to "beta", 3 to "gamma" and so on. I know I could use an ifelse
statement but this would be more tedious than having some way of just converting the factor levels.
If I try for example:
df$P1 <- factor(df$P1, levels=c("alpha","beta","gamma" ))
I get NAs
for values.
Firstly, using cbind
gives a matrix
, not a data.frame
- try:
df <- data.frame( P1=c(1,1,2,2,3), P2=c(2,3,4,4,4) )
Then use labels
for your labels instead of levels
, and set levels
to 1:4
to cover all the possible options in df$P1
and df$P2
df$P1 <- factor(df$P1, levels=1:4, labels=c("alpha","beta","gamma","delta"))
df
# P1 P2
#1 alpha 2
#2 alpha 3
#3 beta 4
#4 beta 4
#5 gamma 4
df$P1
#[1] alpha alpha beta beta gamma
#Levels: alpha beta gamma delta
You could use lapply
to tackle all the variables in one step:
df <- data.frame( P1=c(1,1,2,2,3), P2=c(2,3,4,4,4) )
data.frame(lapply(df,factor,levels=1:4,labels=c("alpha","beta","gamma","delta")))
# P1 P2
#1 alpha beta
#2 alpha gamma
#3 beta delta
#4 beta delta
#5 gamma delta