用于分配新号码的更有用的功能

时间:2018-04-28 01:05:04

标签: r dplyr

我有一个问题,即根据其他列中的条件分配范围非常广泛的数字。

在简单的例子中,我可以像这样描述我的问题

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

我想根据colrow列中的条件创建一个新列,以便

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

    ))))))))))))

  }    
}

当我运行我的功能时,我可以得到

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

assign_z函数太长,因为我的真实数据中有1000个col编号。另外,assign_z列应该以这种系统的方式增加。

即使我有1000列,如何缩短创建相同结果的功能?

2 个答案:

答案 0 :(得分:1)

这对col,row的组合执行密集排名,但将row = 2视为1.我不知道你想要实现什么,但它会重现你的输出并适用于任何长度的数据帧:

assign_z <- function(col, row){
  rank_over = paste0(col, ifelse(row == 2, 1, row))
  final_column = dense_rank(rank_over) - 1
  return(final_column)
}


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

答案 1 :(得分:0)

我不使用R,但根据我所看到的,你不能使用全球计数器 请原谅任何语法问题

    counter <<- 0

    assign_z <- function(col,row){
        #get current value
        returnValue<-counter

        #add to counter if row = 0 or 2 or 3    
        if(row==0|row==2|row==3) { counter <<- counter + 1 }

        #return value before it was incremented
        return(returnValue)
    }

模式似乎是行= 0,2,3

上的数字增量