I got a dataframe with a column called PE with values from 1 to 6:
> head(data)
NID PE
1 4
2 5
3 3
4 4
5 1
6 6
7 2
8 3
9 3
and need to create a new factor column with its values
> data$TYPE = factor(data$PE)
> head(data)
NID PE TYPE
1 4 4
2 5 5
3 3 3
4 4 4
5 1 1
6 6 6
7 2 2
8 3 3
9 3 3
> levels(data$TYPE)
[1] "1" "2" "3" "4" "5" "6"
But the problem is the numbers of levels. The TYPE col must be recode only in 3 levels according to the data$PE values. 1,2 = level "1"; 3,4 = level "2" and 5,6 = level "3", and obtain something like this:
> head(data)
NID PE TYPE
1 4 2
2 5 3
3 3 2
4 4 2
5 1 1
6 6 3
7 2 1
8 3 2
9 3 2
> levels(data$TYPE)
[1] "1" "2" "3"
The solution may be very simple, but I feel that I am stuck and can only create useless junk code, so all help is appreciated.