计算R

时间:2019-04-14 03:20:32

标签: r count repeat find-occurrences

尽管我已经仔细检查过,但我希望以前没有问过这个问题。

基本上,我有一个由21980行和9列组成的数据集。每行由4个值组成:“其他”,“无政府状态”,“稳定性”和“更改”。例如一行: 1个无政府状态稳定状态无政府状态稳定状态稳定其他状态

我想得到一个列表,该列表将为我提供每行(OBJECTID)每种政府价值(无政府状态,其他状态,稳定性,变更)的重复长度”

用上一行说明这一点: ID1其他无政府状态无政府状态稳定性稳定性其他稳定性

我的大输出列表的第一个元素是:       “无政府状态” = 2、2(重复两次,长度为2)       “稳定性” = 1,2(一个稳定性,重复一次长度       二)         other = 1(一个)         更改 = 0(此行无更改)

基本上,我想对整个数据集的每一行都使用它。我提出的代码如下(不幸的是,它不起作用):

matric
k <- 0

test <- list(rec)
test[[1]]$stability <- 1
test[[1]]$stability <- 2

for (j in 1: length(matric$OBJECTID)) {

  for (i in 2:8) {
    if (matric[j,i] == "stability") (
      while (matric[j,i] == matric[j,i+1]) {
        k <- k+1
        biglist[[j]]$stability <- k
        k <- i+k
      }

    )
      if (matric[j,i] == "change") (
      while (matric[j,i] == matric[j,i+1]) {
        k <- k+1
      biglist[[j]]$change <- k
      k <- i+k
      }
    )

     if (matric[j,i] == "anarchy") (
      while (matric[j,i] == matric[j,i+1]) {
        k <- k+1
        biglist[[j]]$anarchy <- k
      k <- i+k
      }
    )
         if (matric[j,i] == "other") (
      while (matric[j,i] == matric[j,i+1]) {
        k <- k+1
      biglist[[j]]$other <- k
      k <- i+k
      }
    )
  }


}

矩阵是data.frame。 biglist是一个包含21980个元素的空列表,每个元素都是具有四个名称的列表,分别是“稳定”,“无政府状态”,“变更”和“其他”。

谢谢。enter image description here

此外,我应该提到的是,我找到了一种使用rle()函数轻松获取行中每个值重复的方法。 尽管如此,这还是行不通的,因为在一天结束时,我真正需要的是数字,该数字与每一行每个值(“无政府状态”,“更改”等)的重复长度相对应,以便能够对它们进行平均

2 个答案:

答案 0 :(得分:1)

这是一个整洁的解决方案,我们将数据提取为长格式,然后进行分组和计数以总结连续的重复值。

library(tidyverse)
# using sample data from below

df %>%
  # convert to long form to help with grouping & counting
  gather(col, val, -OBJECTID) %>%
  arrange(OBJECTID, col) %>%

  # for each OBJECTID row...
  group_by(OBJECTID) %>%
  # Assign a group to each contiguous set of vals by making
  #   a new group whenever val doesn't match the prior one
  mutate(new_grp = val != lag(val, default = ""),
         grp = cumsum(new_grp)) %>%
  ungroup() %>%

  # Count how many in each group & word within each row
  count(OBJECTID, val, grp) %>%
  # Count how many groups of each length by word & row
  count(OBJECTID, val, n) %>%
  rename(grp_length = n,
         count      = nn)
# A tibble: 103,432 x 4
   OBJECTID val       grp_length count
      <int> <chr>          <int> <int>
 1        1 anarchy            1     1
 2        1 change             1     1
 3        1 change             2     1
 4        1 other              1     1
 5        1 stability          1     1
 6        1 stability          3     1
 7        2 anarchy            1     1
 8        2 anarchy            2     1
 9        2 change             1     1
10        2 change             2     1
# … with 103,422 more rows

这意味着对象1具有一个长度为1的“无政府状态”字符串,一个长度为1的“变化”字符串,长度为2,一个长度为1的“其他”字符串,一个长度为1的“稳定性”字符串,以及长度3之一。


样本数据:

df_rows <- 21980
df_columns <- 9
set.seed(42)
df <- tibble(
        OBJECTID = rep(1:df_rows, each = df_columns),
        col = rep(paste0("c", 1:df_columns), times = df_rows),
        val = sample(c("other", "anarchy", "stability", "change"), 
      size = df_rows * df_columns, replace = TRUE)
      ) %>% spread(col, val)

> df
# A tibble: 21,980 x 10
   OBJECTID c1        c2        c3        c4        c5        c6        c7        c8        c9       
      <int> <chr>     <chr>     <chr>     <chr>     <chr>     <chr>     <chr>     <chr>     <chr>    
 1        1 change    change    anarchy   change    stability stability stability other     stability
 2        2 stability anarchy   stability change    anarchy   anarchy   change    change    other    
 3        3 anarchy   stability change    other     change    change    other     stability anarchy  
 4        4 change    anarchy   change    stability change    anarchy   stability other     change   
 5        5 other     other     change    stability anarchy   anarchy   other     change    anarchy  
 6        6 change    change    stability change    stability anarchy   anarchy   anarchy   change   
 7        7 other     stability stability other     anarchy   stability stability change    change   
 8        8 stability change    other     anarchy   change    stability other     other     other    
 9        9 other     anarchy   other     stability other     anarchy   stability other     stability
10       10 other     anarchy   stability change    stability other     other     other     anarchy 
# … with 21,970 more rows

答案 1 :(得分:0)

假设您有一个9列的数据帧df,看起来像这样,并且我已经正确理解了您的问题

str(df)

 $ OBJECTID: Factor w/ 5 levels "1","2","3","4",..: 1 2 3 4 5
 $ REP1    : chr  "anarchy" "change" "stability" "other" ...
 $ REP2    : chr  "anarchy" "stability" "anarchy" "change" ...
 $ REP3    : chr  "other" "anarchy" "stability" "anarchy" ...
 $ REP4    : chr  "change" "stability" "change" "anarchy" ...
 $ REP5    : chr  "anarchy" "stability" "stability" "other" ...
 $ REP6    : chr  "other" "anarchy" "stability" "stability" ...
 $ REP7    : chr  "stability" "stability" "anarchy" "stability" ...
 $ REP8    : chr  "change" "anatchy" "change" "chang

您可以使用tidyr来重塑它,并为每个OBJECTID计算每个政府的出现次数。

library(tidyr)
df %>% 
  gather(rep, gov, 2:9) %>% 
  group_by(OBJECTID, gov) %>% 
  summarize(count = n())

您会得到类似的东西

OBJECTID  gov       count
1        anarchy    3       
1        change     2       
1        other      2       
1        stability  1       
2        anarchy    3       
2        change     1       
2        stability  4       
3        anatchy    2