如何根据一列添加增量值?

huangapple go评论70阅读模式
英文:

How to add incremental value base on a column?

问题

例如,如何获取列“rank”的值?
在这里,数据帧中的每2条记录的值递增。但实际上可以是任意数量的记录。

我找到了一些类似的帖子,但它们不完全符合我所需的。

月份 排名
1 1
2 1
3 2
4 2
5 3
6 3
7 4
8 4
英文:

For example, how do I obtain the value for the column "rank"?
Here, the value increases by every 2 records in the data frame. Thought it could be any # of records instead of 2.

I did found a few similar posts, but they are not exactly what I need.

Months rank
1 1
2 1
3 2
4 2
5 3
6 3
7 4
8 4

答案1

得分: 1

使用repeach=参数根据nrow(dat)/n进行操作。

f <- function(dat, n) {
  stopifnot(nrow(dat) %% n == 0)  ## 为了安全起见
  rep(seq_len(nrow(dat)/n), each=n)
}

f(dat, 1)
# [1] 1 2 3 4 5 6 7 8

f(dat, 2)
# [1] 1 1 2 2 3 3 4 4

f(dat, 3)
# Error in f(dat, 3): nrow(dat)%%n == 0 is not TRUE

f(dat, 4)
# [1] 1 1 1 1 2 2 2 2

f(dat, nrow(dat))
# [1] 1 1 1 1 1 1 1 1

要获得不等大小的组,你可以实现一个override=选项。

f2 <- function(dat, n, override=FALSE) {
  if (!override) {
    stopifnot(nrow(dat) %% n == 0)
    rep(seq_len(nrow(dat)/n), each=n)
  } else {
    rep(seq_len(nrow(dat)), each=n)[seq_len(nrow(dat))]
  }
}

f2(dat, 3, override=TRUE)
# [1] 1 1 1 2 2 2 3 3

或者,稍微更加优雅:

f3 <- function(dat, n) {
  sort.int(rep_len(1:n, nrow(dat)))
}

f3(dat, 0)
# [1] 0 0 0 0 1 1 1 1

f3(dat, 1)
# [1] 1 1 1 1 1 1 1 1

f3(dat, 2)
# [1] 1 1 1 1 2 2 2 2

f3(dat, 3)
# [1] 1 1 1 2 2 2 3 3

f3(dat, 8)
# [1] 1 2 3 4 5 6 7 8

f3(dat, 9)
# [1] 1 2 3 4 5 6 7 8

f3(dat, -1)
# [1] -1 -1  0  0  0  1  1  1

数据:

dat <- structure(list(Months = 1:8, rank = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L)), class = "data.frame", row.names = c(NA, -8L))
英文:

Using rep and each= argument according to nrow(dat)/n.

f &lt;- \(dat, n) {
  stopifnot(nrow(dat) %% n == 0)  ## for safety
  rep(seq_len(nrow(dat)/n), each=n)
}

f(dat, 1)
# [1] 1 2 3 4 5 6 7 8

f(dat, 2)
# [1] 1 1 2 2 3 3 4 4

f(dat, 3)
# Error in f(dat, 3) : nrow(dat)%%n == 0 is not TRUE

f(dat, 4)
# [1] 1 1 1 1 2 2 2 2

f(dat, nrow(dat))
# [1] 1 1 1 1 1 1 1 1

To also get unequal groups, you could implement an override= option.

f2 &lt;- \(dat, n, override=FALSE) {
  if (!override) {
    stopifnot(nrow(dat) %% n == 0)
    rep(seq_len(nrow(dat)/n), each=n)
  } else {
    rep(seq_len(nrow(dat)), each=n)[seq_len(nrow(dat))]
  }
}

f2(dat, 3, override=TRUE)
# [1] 1 1 1 2 2 2 3 3

Or, slightly more elegant:

f3 &lt;- \(dat, n) {
  sort.int(rep_len(1:n, nrow(dat)))
}

f3(dat, 0)
# [1] 0 0 0 0 1 1 1 1

f3(dat, 1)
# [1] 1 1 1 1 1 1 1 1

f3(dat, 2)
# [1] 1 1 1 1 2 2 2 2

f3(dat, 3)
# [1] 1 1 1 2 2 2 3 3

f3(dat, 8)
# [1] 1 2 3 4 5 6 7 8

f3(dat, 9)
# [1] 1 2 3 4 5 6 7 8

f3(dat, -1)
# [1] -1 -1  0  0  0  1  1  1

Data:

dat &lt;- structure(list(Months = 1:8, rank = c(1L, 1L, 2L, 2L, 3L, 3L, 
4L, 4L)), class = &quot;data.frame&quot;, row.names = c(NA, -8L))

答案2

得分: 1

以下是已翻译的内容:

假设我们有如下的数据:

然后我们执行以下操作:

df <- data.frame(letters=c(LETTERS[1:8]), Months=c(1:8)) %>%
  mutate(rank1=(Months %% 2), 
         rank2=ifelse(rank1==1,Months, NA_real_)) %>%
  fill(rank2) %>%
  mutate(rank=data.table::rleid(rank2)) %>%
  select(-c('rank1','rank2'))  

输出结果为:

  letters Months rank
1       A      1    1
2       B      2    1
3       C      3    2
4       D      4    2
5       E      5    3
6       F      6    3
7       G      7    4
8       H      8    4
英文:

For suppose we have a data as below

  letters 
1       A 
2       B 
3       C 
4       D 
5       E 
6       F 
7       G 
8       H 

Then we do something as below

df &lt;- data.frame(letters=c(LETTERS[1:8]), Months=c(1:8)) %&gt;% 
  mutate(rank1=(Months %% 2), 
         rank2=ifelse(rank1==1,Months, NA_real_)) %&gt;% 
  fill(rank2) %&gt;% 
  mutate(rank=data.table::rleid(rank2)) %&gt;% 
  select(-c(&#39;rank1&#39;,&#39;rank2&#39;))  

# output
  letters Months rank
1       A      1    1
2       B      2    1
3       C      3    2
4       D      4    2
5       E      5    3
6       F      6    3
7       G      7    4
8       H      8    4

答案3

得分: 0

请尝试以下代码:

df <- data.frame(Months=c(1:8)) %>% 
  mutate(rank1=(Months %% 2), 
         rank2=ifelse(rank1==1,Months, NA_real_),
  ) %>% 
  fill(rank2) %>% 
  mutate(rank=data.table::rleid(rank2)) %>% 
  select(-c('rank1','rank2'))

# 输出结果
  Months rank
1      1    1
2      2    1
3      3    2
4      4    2
5      5    3
6      6    3
7      7    4
8      8    4
英文:

Please try the below code,

df &lt;- data.frame(Months=c(1:8)) %&gt;% mutate(rank1=(Months %% 2), 
                                     rank2=ifelse(rank1==1,Months, NA_real_),
                                     ) %&gt;% fill(rank2) %&gt;% 
  mutate(rank=data.table::rleid(rank2)) %&gt;% select(-c(&#39;rank1&#39;,&#39;rank2&#39;))

# output
  Months rank
1      1    1
2      2    1
3      3    2
4      4    2
5      5    3
6      6    3
7      7    4
8      8    4

huangapple
  • 本文由 发表于 2023年6月26日 00:57:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定