I have a question about assigning a very wide range of numbers based on conditions in other columns.
In simple example I can describe my problem like this
df <- data.frame(col=rep(seq(0,3),each=4), row = c(seq(0,3)))
> df
col row
1 0 0
2 0 1
3 0 2
4 0 3
5 1 0
6 1 1
7 1 2
8 1 3
9 2 0
10 2 1
11 2 2
12 2 3
13 3 0
14 3 1
15 3 2
16 3 3
I would like to create a new column based on the conditions in col and row column such that
assign_z <- function(col,row){
ifelse(col==0&row<=0, 0, #0 is the assigned number to assign_z column
ifelse(col==0&row>0&row<=2, 1, #1 is the assigned number to assign_z column
ifelse(col==0&row>=3,2, #2 is the assigned number to assign_z column
ifelse(col==1&row<=0,3, #3 is the assigned number to assign_z column
ifelse(col==1&row>0&row<=2,4, #4 is the assigned number to assign_z column
ifelse(col==1&row>=3,5, #5 is the assigned number to assign_z column
ifelse(col==2&row<=0,6, #6 is the assigned number to assign_z column
ifelse(col==2&row>0&row<=2,7, #7 is the assigned number to assign_z column
ifelse(col==2&row>=3,8, #8 is the assigned number to assign_z column
ifelse(col==3&row<=0,9, #9 is the assigned number to assign_z column
ifelse(col==3&row>0&row<=2,10, #10 is the assigned number to assign_z column
ifelse(col==3&row>=3,11,NA #11 is the assigned number to assign_z column
))))))))))))
}
}
and when I run my function I can get
library(dplyr)
df%>%
mutate(assign_z=assign_z(col,row))
col row assign_z
1 0 0 0
2 0 1 1
3 0 2 1
4 0 3 2
5 1 0 3
6 1 1 4
7 1 2 4
8 1 3 5
9 2 0 6
10 2 1 7
11 2 2 7
12 2 3 8
13 3 0 9
14 3 1 10
15 3 2 10
16 3 3 11
but assign_z function will be too long as I have 1000 col number in my real data. In addition, assign_z column should be increased in this systematical manner.
How can shorten the function to create same result even if I have 1000 columns ?