【问题标题】:For loop to iterate through dplyr pipeFor循环遍历dplyr管道
【发布时间】:2020-06-26 13:17:02
【问题描述】:

我正在尝试获取数据框中每一行的条目总数,以便以后压缩这些字段。

但是数据框有超过 60 行,写下面 60 次是非常低效的

df %>%
    group_by(colname) %>%
    count() %>%
    arrange(desc(n))

有没有办法我可以编写一个 for 循环来遍历数据框中的所有名称并为每个名称生成管道函数结果?我试过了

for (i in colnames(df)) {

df %>%
    group_by(colname) %>%
    count() %>%
    arrange(desc(n))

}

但我收到“我未知”错误。任何帮助将不胜感激。

【问题讨论】:

  • 在你的算法中用 i 替换 colname。
  • 你可以按照@grouah 的建议去做,但这不是一个非常整洁的解决方案。如果没有最小的、可重现的示例,我无法确定,但我相信您可能想要使用 summarize 函数 across 要计算的列并使用 n 函数。如果您想要更好的答案,请提供最少的可重现示例和所需的输出
  • 对此的 tidyverse 解决方案可能是使用 pivot_longer() 重塑数据,然后使用 group_by() 和 count() - 将有助于获得一个可重复的示例

标签: r dplyr


【解决方案1】:

如果我理解正确,您想计算每一列中唯一元素的出现次数,还是我完全弄错了?为什么你不只是使用一些应用函数和表格的组合?

set.seed(101)
df <- data.frame("x" = 1:20, "y" = LETTERS[sample(1:26, 20, replace = TRUE)], "z" = letters[sample(1:26, 20, replace = TRUE)])
l <- sapply(df, table)
lapply(l, sort, decreasing = T)

【讨论】:

  • 虽然这也有效,但输出很混乱,而且 tidyverse 让我的经验更容易解释。不过还是谢谢
【解决方案2】:

你可以试试这个:

#Data
df <- iris
#Create list
List <- list()
#Compute
for (colname in colnames(df)) {
  
  List[[colname]]<- df %>%
    group_by(df[,colname]) %>%
    count() %>%
    arrange(desc(n))
  
}
#Print
List

$Sepal.Length
# A tibble: 35 x 2
# Groups:   df[, colname] [35]
   `df[, colname]`     n
                   <dbl> <int>
 1                   5      10
 2                   5.1     9
 3                   6.3     9
 4                   5.7     8
 5                   6.7     8
 6                   5.5     7
 7                   5.8     7
 8                   6.4     7
 9                   4.9     6
10                   5.4     6
# ... with 25 more rows

$Sepal.Width
# A tibble: 23 x 2
# Groups:   df[, colname] [23]
   `df[, colname]`     n
                   <dbl> <int>
 1                   3      26
 2                   2.8    14
 3                   3.2    13
 4                   3.4    12
 5                   3.1    11
 6                   2.9    10
 7                   2.7     9
 8                   2.5     8
 9                   3.3     6
10                   3.5     6
# ... with 13 more rows

$Petal.Length
# A tibble: 43 x 2
# Groups:   df[, colname] [43]
   `df[, colname]`     n
                   <dbl> <int>
 1                   1.4    13
 2                   1.5    13
 3                   4.5     8
 4                   5.1     8
 5                   1.3     7
 6                   1.6     7
 7                   5.6     6
 8                   4       5
 9                   4.7     5
10                   4.9     5
# ... with 33 more rows

$Petal.Width
# A tibble: 22 x 2
# Groups:   df[, colname] [22]
   `df[, colname]`     n
                   <dbl> <int>
 1                   0.2    29
 2                   1.3    13
 3                   1.5    12
 4                   1.8    12
 5                   1.4     8
 6                   2.3     8
 7                   0.3     7
 8                   0.4     7
 9                   1       7
10                   2       6
# ... with 12 more rows

$Species
# A tibble: 3 x 2
# Groups:   df[, colname] [3]
  `df[, colname]`     n
  <fct>                 <int>
1 setosa                   50
2 versicolor               50
3 virginica                50

【讨论】:

  • 当我运行代码时出现错误“列 df[, colname] is of unsupported class data.frame”
  • @OisinBrannock 这取决于您的数据,如果您添加到您的原始问题dput(Data),我们可以提供帮助。或者尝试更新dplyr
猜你喜欢
  • 2020-11-17
  • 1970-01-01
  • 2016-03-03
  • 1970-01-01
  • 2020-12-02
  • 2017-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多