【问题标题】:Column sum in mutate function in R [duplicate]R中变异函数中的列总和[重复]
【发布时间】:2021-05-12 16:28:11
【问题描述】:

我有一个虹膜,我需要将每个虹膜除以相应列的总和。所以我正在考虑使用 mutate 以便所有操作立即发生。但我越来越错误了

iris1 <- head(iris[1:4])
iris1 %>% mutate(across(c(1:4), ~.-colSums(.)))
Error: Problem with `mutate()` input `..1`.
x 'x' must be an array of at least two dimensions
ℹ Input `..1` is `across(c(1:4), ~. - colSums(.))`.
Run `rlang::last_error()` to see where the error occurred.

预期(因为 29.7 是 1 列的总和。其他列类似)

       Sepal.Length         Sepal.Width Petal.Length Petal.Width
1          5.1/29.7             3.5          1.4         0.2
2          4.9/29.7             3.0          1.4         0.2
3          4.7/29.7             3.2          1.3         0.2
4          4.6/29.7             3.1          1.5         0.2
5          5.0/29.7             3.6          1.4         0.2
6          5.4/29.7             3.9          1.7         0.4

【问题讨论】:

    标签: r


    【解决方案1】:

    你很亲密。 这是你想要的吗?

    head(iris[1:4]) %>% summarise(across(.cols = c(1:4), .fns = function(x) {x/sum(x)}))
    

    输出:

     Sepal.Length Sepal.Width Petal.Length Petal.Width
    1    0.1717172   0.1724138    0.1609195   0.1428571
    2    0.1649832   0.1477833    0.1609195   0.1428571
    3    0.1582492   0.1576355    0.1494253   0.1428571
    4    0.1548822   0.1527094    0.1724138   0.1428571
    5    0.1683502   0.1773399    0.1609195   0.1428571
    6    0.1818182   0.1921182    0.1954023   0.2857143
    

    【讨论】:

    • @user11740857 如果您认为由于colSums(head(iris[1:4])) 的实际输出而出现错误。也许比较 colSums(head(iris[1:4]))head(iris[1:4]) %&gt;% summarise(across(.cols = c(1:4), .fns = function(x) {sum(x)})) 可以显示您的代码产生错误的原因
    【解决方案2】:

    更简洁的解决方案:

    iris1 %>% 
      summarise(across(everything(), ~./sum(.)))
     Sepal.Length Sepal.Width Petal.Length Petal.Width
    1    0.1717172   0.1724138    0.1609195   0.1428571
    2    0.1649832   0.1477833    0.1609195   0.1428571
    3    0.1582492   0.1576355    0.1494253   0.1428571
    4    0.1548822   0.1527094    0.1724138   0.1428571
    5    0.1683502   0.1773399    0.1609195   0.1428571
    6    0.1818182   0.1921182    0.1954023   0.2857143
    

    【讨论】:

      【解决方案3】:

      基础

      iris1/sapply(iris1, sum)
      
        Sepal.Length Sepal.Width Petal.Length Petal.Width
      1    0.1717172   0.4022989   0.04713805 0.022988506
      2    0.2413793   2.1428571   0.06896552 0.142857143
      3    0.5402299   0.1077441   0.14942529 0.006734007
      4    3.2857143   0.1527094   1.07142857 0.009852217
      5    0.1683502   0.4137931   0.04713805 0.022988506
      6    0.2660099   2.7857143   0.08374384 0.285714286
      

      【讨论】:

        【解决方案4】:

        一个简单的基本 R 选项是 prop.table

        > list2DF(Map(prop.table, iris1))
          Sepal.Length Sepal.Width Petal.Length Petal.Width
        1    0.1717172   0.1724138    0.1609195   0.1428571
        2    0.1649832   0.1477833    0.1609195   0.1428571
        3    0.1582492   0.1576355    0.1494253   0.1428571
        4    0.1548822   0.1527094    0.1724138   0.1428571
        5    0.1683502   0.1773399    0.1609195   0.1428571
        6    0.1818182   0.1921182    0.1954023   0.2857143
        

        mutate_all

        > iris1 %>%
        +   mutate_all(prop.table)
          Sepal.Length Sepal.Width Petal.Length Petal.Width
        1    0.1717172   0.1724138    0.1609195   0.1428571
        2    0.1649832   0.1477833    0.1609195   0.1428571
        3    0.1582492   0.1576355    0.1494253   0.1428571
        4    0.1548822   0.1527094    0.1724138   0.1428571
        5    0.1683502   0.1773399    0.1609195   0.1428571
        6    0.1818182   0.1921182    0.1954023   0.2857143
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-10-03
          • 1970-01-01
          • 1970-01-01
          • 2016-10-15
          • 2014-06-12
          • 1970-01-01
          • 2021-11-16
          • 1970-01-01
          相关资源
          最近更新 更多