【问题标题】:How to create a new column with mutate() in function of all others without namimg them如何使用 mutate() 在所有其他列的函数中创建一个新列而不命名它们
【发布时间】:2021-04-07 15:50:53
【问题描述】:

我有一个tibble(我已经在使用tidyverse),但由于它很大,让我们使用稍作修改的iris 数据集:

iris %>%
  as_tibble() %>%
  select(-5) 

我想创建另一列,它是这些列的平方和的平方根(您可能会注意到,它们都是dbl)。但是,这种方法应该泛化为任意数量的列和任意名称,这不仅是因为原始数据集有更多的列,还因为它更优雅。

此代码有效,但特定于具有以下名称的这些列:

iris %>%
  as_tibble() %>%
  select(-5) %>%
  mutate(linear = sqrt(Sepal.Length^2+Sepal.Width^2+Petal.Length^2+Petal.Width^2)) %>%
  arrange(linear)

这给出了预期的输出:

> iris %>%
+   as_tibble() %>%
+   select(-5) %>%
+   mutate(linear = sqrt(Sepal.Length^2+Sepal.Width^2+Petal.Length^2+Petal.Width^2)) %>%
+   arrange(linear)
# A tibble: 150 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width linear
          <dbl>       <dbl>        <dbl>       <dbl>  <dbl>
 1          4.5         2.3          1.3         0.3   5.23
 2          4.3         3            1.1         0.1   5.36
 3          4.4         2.9          1.4         0.2   5.46
 4          4.4         3            1.3         0.2   5.49
 5          4.4         3.2          1.3         0.2   5.60
 6          4.6         3.1          1.5         0.2   5.75
 7          4.6         3.2          1.4         0.2   5.78
 8          4.8         3            1.4         0.1   5.83
 9          4.7         3.2          1.3         0.2   5.84
10          4.8         3            1.4         0.3   5.84
# … with 140 more rows

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    这对你有用吗?

    iris %>%
    +   as_tibble() %>%
    +   select(-5) %>% mutate(linear = sqrt(rowSums(iris[-5]**2))) %>% arrange(linear)
    # A tibble: 150 x 5
       Sepal.Length Sepal.Width Petal.Length Petal.Width linear
              <dbl>       <dbl>        <dbl>       <dbl>  <dbl>
     1          4.5         2.3          1.3         0.3   5.23
     2          4.3         3            1.1         0.1   5.36
     3          4.4         2.9          1.4         0.2   5.46
     4          4.4         3            1.3         0.2   5.49
     5          4.4         3.2          1.3         0.2   5.60
     6          4.6         3.1          1.5         0.2   5.75
     7          4.6         3.2          1.4         0.2   5.78
     8          4.8         3            1.4         0.1   5.83
     9          4.7         3.2          1.3         0.2   5.84
    10          4.8         3            1.4         0.3   5.84
    # ... with 140 more rows
    

    【讨论】:

    • 在这里,我正在用purrr 函数破解我的大脑,这个超级简单而优雅的答案潜伏在我的掌握之外......是的,它有效!只要 StackOverflow 允许,我就会接受它!
    【解决方案2】:

    您也可以使用此解决方案:

    library(dplyr)
    library(purrr)
    
    iris %>%
      as_tibble() %>%
      select(-5) %>%
      mutate(linear = pmap(., ~ sqrt(sum(c(...) ^ 2)))) %>%
      unnest(linear) %>%
      arrange(linear)
    
    # A tibble: 150 x 5
       Sepal.Length Sepal.Width Petal.Length Petal.Width linear
              <dbl>       <dbl>        <dbl>       <dbl>  <dbl>
     1          4.5         2.3          1.3         0.3   5.23
     2          4.3         3            1.1         0.1   5.36
     3          4.4         2.9          1.4         0.2   5.46
     4          4.4         3            1.3         0.2   5.49
     5          4.4         3.2          1.3         0.2   5.60
     6          4.6         3.1          1.5         0.2   5.75
     7          4.6         3.2          1.4         0.2   5.78
     8          4.8         3            1.4         0.1   5.83
     9          4.7         3.2          1.3         0.2   5.84
    10          4.8         3            1.4         0.3   5.84
    # ... with 140 more rows
    
    

    如果您想知道如何将purrr 函数应用于此类问题,请查看亲爱的akrun 提供的this 综合回答。

    【讨论】:

      【解决方案3】:

      这是一个有趣的问题。我希望找到一个 map_dbl/pmap_dbl 解决方案,尽管我最终得到了这个:注意rowwise()

      iris %>%
        as_tibble() %>%
        select(-5) %>%
        rowwise() %>% 
        mutate(linear = sqrt(sum(cur_data()^2))) %>%
        arrange(linear)
      
      # A tibble: 150 x 5
      # Rowwise: 
         Sepal.Length Sepal.Width Petal.Length Petal.Width linear
                <dbl>       <dbl>        <dbl>       <dbl>  <dbl>
       1          4.5         2.3          1.3         0.3   5.23
       2          4.3         3            1.1         0.1   5.36
       3          4.4         2.9          1.4         0.2   5.46
       4          4.4         3            1.3         0.2   5.49
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-10
        • 2021-01-16
        • 1970-01-01
        • 2019-06-28
        • 2020-10-06
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        相关资源
        最近更新 更多