I have multiple columns and I want to code them sequentially. Here is a sample of the columns:
df<-read.table(text=" A M Z X
124321 33333 123 1309
234543 12121 33 1308
130991 200EE 123 1308
130911 200EE 123 1309
124321 12121 33 1309
234543 33333 232 1309", h=T)
I want to get this table:
df1<-read.table(text=" Group1 Group2 Group3 Group4
1 6 9 12
4 5 8 11
3 7 9 11
2 7 9 12
1 5 8 12
4 6 10 12
", h=T)
I have used the following basic codes, but they are not reliable, especially when the columns are increased based on my experiences.
df$Group1 <- as.integer(as.factor(df$A))
df$Group2 <- as.integer(as.factor(df$M)) + max(df$Group1)
df$Group3 <- as.integer(as.factor(df$Z)) + max(df$Group2)
df$Group4 <- as.integer(as.factor(df$X)) + max(df$Group3)
Is there a better and more reliable solution to get my table?