Let's imagine I have a data frame containing 2 types information (X# and Y#).
df = data.frame(matrix(rnorm(600), nrow=100))
colnames(df) <- c("X1", "X2", "Y1", "Y2", "Y3", "Y4")
I use two columns (below X1 and Y1) to group them in 9 categories (each column being splitted in 3 categories containing 1/3 or the rows) and store them in a new column cat11 (I deeply apologize for the poor code I show you, but I am a just a beginner in R).
df$tmpx <- cut2(df$X1, g=3)
levels(df$tmpx) <- c(1,2,3)
df$tmpy <- cut2(df$Y1, g=3)
levels(df$tmpy) <- c(1,2,3)
enum <- 1
for (x in sort(unique(df$tmpx)))
{
for (y in sort(unique(df$tmpy)))
{
print(enum)
df$cat11[df$tmpx == x & df$tmpy == y] <- enum
enum <- enum + 1
}
}
What I am struggling to do now is to run this code for a selection of other combinations (e.g X1,Y4 > cat14; X2,Y1 > cat21; X2,Y3 > cat23).
I have been trying using function as well as lapply, but unsuccessfully yet. I think I am missing something obvious.
Any help would be much appreciated.