对字符向量的内容进行计数更改 [英] Count changes to contents of a character vector

查看:86
本文介绍了对字符向量的内容进行计数更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 data_frame 其中一个字符变量 x 在时间上发生变化。我想计算它改变的次数,并用这个计数填充一个新的向量。

I have a data_frame where a character variable x changes in time. I want to count the number of times it changes, and fill a new vector with this count.

df <- data_frame(
  x = c("a", "a", "b", "b", "c", "b"),
  wanted = c(1, 1, 2, 2, 3, 4)
)
  x wanted
1 a      1
2 a      1
3 b      2
4 b      2
5 c      3
6 b      4

这是类似的,但不同于 rle(df $ x),将返回

This is similar to, but different from rle(df$x), which would return

Run Length Encoding
  lengths: int [1:4] 2 2 1 1
  values : chr [1:4] "a" "b" "c" "b"

我可以尝试 rep()那个输出。我也尝试了这个,这是非常接近的,但不是因为我不能马上知道的原因:

I could try to rep() that output. I have also tried this, which is awfully close, but not for reasons I can't figure out immediately:

 df %>% mutate( 
   try_1 = cumsum(ifelse(x == lead(x) | is.na(lead(x)), 1, 0)) 
   )
Source: local data frame [6 x 3]

  x wanted try_1
1 a      1     1
2 a      1     1
3 b      2     2
4 b      2     2
5 c      3     2
6 b      4     3

似乎应该有一个功能,直接做到这一点,我刚才没有找到我的经验。

It seems like there should be a function that does this directly, that I just haven't found in my experience.

推荐答案

尝试这个 dplyr 代码:

df %>%
  mutate(try_1 = cumsum(ifelse(x != lag(x) | is.na(lag(x)), 1, 0)))

  x wanted try_1
1 a      1     1
2 a      1     1
3 b      2     2
4 b      2     2
5 c      3     3
6 b      4     4

你的意思是:增加计数如果值与以下行的值相同,或者如果以下行的值为NA。

Yours was saying: increment the count if a value is the same as the following row's value, or if the following row's value is NA.

这表示:如果此行上的变量是与上一行不同,或者如果前一行没有(例如,第1行)。

This says: increment the count if the variable on this row either is different than the one on the previous row, or if there wasn't one on the previous row (e.g., row 1).

这篇关于对字符向量的内容进行计数更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆