找到数据帧每行中最长字符串的长度

时间:2018-02-12 16:25:54

标签: r

我想找到一个模式后面最长字符串的长度。让我们说我有这个数据帧,我想找到行的长度是序列" a"正在重复我怎么找到它?

id = c(1, 2, 3,4,5)
A = c("a","a","a","a","a")
B = c("a","a","b","a","d")
C = c("b","a","c","a","a")
D = c("a","a","a","b","c")
E = c("a","a","e","c","a")

df = data.frame(id,A,B,C,D,E,stringsAsFactors=FALSE)
df$Count = c(2,5,1,3,1)

  id A B C D E Count
  1 a a b a a     2
  2 a a a a a     5
  3 a b c a e     1
  4 a a a b c     3
  5 a d a c a     1

1 个答案:

答案 0 :(得分:1)

您可以使用rle游程编码)。

    rles = apply(df[2:6], 1, rle)
    result = sapply(rles, function(x) max(x$lengths[x$values == "a"]))
    df$new_count = result
    df
    #   id A B C D E Count new_count
    # 1  1 a a b a a     2         2
    # 2  2 a a a a a     5         5
    # 3  3 a b c a e     1         1
    # 4  4 a a a b c     3         3
    # 5  5 a d a c a     1         1

如果您搜索" [r] rle"请参阅本网站上的?rle或许多其他问题。有关其他详细信息。