如何使用现有的数值向量来派生新变量

huangapple go评论100阅读模式
英文:

how to derive new variables from existing using a vector of values

问题

这是我所拥有的数据:

  1. temp <- tibble::tribble(
  2. ~x1, ~x2, ~x3, ~x4, ~x5,
  3. 1, 2, 3, 'AA', 'BB',
  4. 2, 3, 4, 'AB', 'CC',
  5. 3, 4, 5, 'AC', 'DD',
  6. 4, 5, 6, 'AD', 'EE',
  7. 5, 6, 7, 'AE', 'FF',
  8. 6, 7, 8, 'AF', 'GG'
  9. ) %>% dplyr::mutate(dplyr::across(x4,as.character), dplyr::across(x5,as.character))

我想通过与值为c(1,2,3)的temp_val向量相乘来导出新变量'px1'、'px2'、'px3'。

  1. temp_val <- c(1,2,3)

我尝试了以下代码:

  1. temp_val <- c(1,2,3)
  2. nam1 <- c('px1','px2','px3')
  3. temp5 <- temp %>% mutate(across(where(is.numeric), ~ . * temp_val, .names = '{nam1}'))

我得到了以下数据:

如何使用现有的数值向量来派生新变量

我的期望是将向量值视为列,并使用这些列创建多个变量。

如何使用现有的数值向量来派生新变量

英文:

here is the data what i have

  1. temp &lt;- tibble::tribble(
  2. ~x1, ~x2, ~x3, ~x4, ~x5,
  3. 1, 2, 3, &#39;AA&#39;, &#39;BB&#39;,
  4. 2, 3, 4, &#39;AB&#39;, &#39;CC&#39;,
  5. 3, 4, 5, &#39;AC&#39;, &#39;DD&#39;,
  6. 4, 5, 6, &#39;AD&#39;, &#39;EE&#39;,
  7. 5, 6, 7, &#39;AE&#39;, &#39;FF&#39;,
  8. 6, 7, 8, &#39;AF&#39;, &#39;GG&#39;
  9. ) %&gt;% dplyr::mutate(dplyr::across(x4,as.character), dplyr::across(x5,as.character))

I want to derive new variables 'px1', 'px2', 'px3' by multiplying with a vector temp_val with values c(1,2,3)

  1. temp_val &lt;- c(1,2,3)

I tried the below code

  1. temp_val &lt;- c(1,2,3)
  2. nam1 &lt;- c(&#39;px1&#39;,&#39;px2&#39;,&#39;px3&#39;)
  3. temp5 &lt;- temp %&gt;% mutate(across(where(is.numeric), ~ . * temp_val, .names = &#39;{nam1}&#39;))

I get below data

如何使用现有的数值向量来派生新变量

my expectation, is to have vector values considered as columns and multiple variables with those columns

如何使用现有的数值向量来派生新变量

答案1

得分: 3

我们可以创建一个命名向量,然后根据从列名循环追加p得到的名称,对temp_val的元素进行子集化。

  1. library(dplyr)
  2. library(stringr)
  3. names(temp_val) <- nam1
  4. temp %>%
  5. mutate(across(where(is.numeric), ~
  6. .x * temp_val[str_c("p", cur_column())], .names = "p{.col}"))

-output

  1. # A tibble: 6 × 8
  2. x1 x2 x3 x4 x5 px1 px2 px3
  3. <dbl> <dbl> <dbl> <chr> <chr> <dbl> <dbl> <dbl>
  4. 1 1 2 3 AA BB 1 4 9
  5. 2 2 3 4 AB CC 2 6 12
  6. 3 3 4 5 AC DD 3 8 15
  7. 4 4 5 6 AD EE 4 10 18
  8. 5 5 6 7 AE FF 5 12 21
  9. 6 6 7 8 AF GG 6 14 24

或者如果名称顺序相同,可以使用map2的另一种选项。

  1. library(purrr)
  2. map2_dfc(temp %>%
  3. select(where(is.numeric)), temp_val, `*`) %>%
  4. setNames(nam1)%>%
  5. bind_cols(temp, .)
  6. # A tibble: 6 × 8
  7. x1 x2 x3 x4 x5 px1 px2 px3
  8. <dbl> <dbl> <dbl> <chr> <chr> <dbl> <dbl> <dbl>
  9. 1 1 2 3 AA BB 1 4 9
  10. 2 2 3 4 AB CC 2 6 12
  11. 3 3 4 5 AC DD 3 8 15
  12. 4 4 5 6 AD EE 4 10 18
  13. 5 5 6 7 AE FF 5 12 21
  14. 6 6 7 8 AF GG 6 14 24
英文:

We may create a named vector and then subset the elements of the temp_val based on the name derived from appending p to the column name looped (cur_column())

  1. library(dplyr)
  2. library(stringr)
  3. names(temp_val) &lt;- nam1
  4. temp %&gt;%
  5. mutate(across(where(is.numeric), ~
  6. .x * temp_val[str_c(&quot;p&quot;, cur_column())], .names = &quot;p{.col}&quot;))

-output

  1. # A tibble: 6 &#215; 8
  2. x1 x2 x3 x4 x5 px1 px2 px3
  3. &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  4. 1 1 2 3 AA BB 1 4 9
  5. 2 2 3 4 AB CC 2 6 12
  6. 3 3 4 5 AC DD 3 8 15
  7. 4 4 5 6 AD EE 4 10 18
  8. 5 5 6 7 AE FF 5 12 21
  9. 6 6 7 8 AF GG 6 14 24

Or another option if the names are in the same order, then use map2

  1. library(purrr)
  2. map2_dfc(temp %&gt;%
  3. select(where(is.numeric)), temp_val, `*`) %&gt;%
  4. setNames(nam1)%&gt;%
  5. bind_cols(temp, .)
  6. # A tibble: 6 &#215; 8
  7. x1 x2 x3 x4 x5 px1 px2 px3
  8. &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  9. 1 1 2 3 AA BB 1 4 9
  10. 2 2 3 4 AB CC 2 6 12
  11. 3 3 4 5 AC DD 3 8 15
  12. 4 4 5 6 AD EE 4 10 18
  13. 5 5 6 7 AE FF 5 12 21
  14. 6 6 7 8 AF GG 6 14 24

答案2

得分: 2

除了 @akrun 的回答之外,您还可以使用逐行计算:

  1. library(dplyr)
  2. library(tidyr)
  3. temp <- tibble::tribble(
  4. ~x1, ~x2, ~x3, ~x4, ~x5,
  5. 1, 2, 3, 'AA', 'BB',
  6. 2, 3, 4, 'AB', 'CC',
  7. 3, 4, 5, 'AC', 'DD',
  8. 4, 5, 6, 'AD', 'EE',
  9. 5, 6, 7, 'AE', 'FF',
  10. 6, 7, 8, 'AF', 'GG'
  11. ) %>% dplyr::mutate(dplyr::across(x4, as.character), dplyr::across(x5, as.character))
  12. temp_val <- c(1, 2, 3)
  13. nam1 <- c('px1', 'px2', 'px3')
  14. temp %>%
  15. rowwise() %>%
  16. transmute(newvars = list(c_across(where(is.numeric)) * temp_val)) %>%
  17. unnest_wider(newvars) %>%
  18. setNames(nam1) %>%
  19. bind_cols(temp, .)

在 2023-01-08 由 reprex 包 (v2.0.1) 创建

英文:

In addition to @akrun's answer, you could also use a row-wise calculation:

  1. library(dplyr)
  2. #&gt;
  3. #&gt; Attaching package: &#39;dplyr&#39;
  4. #&gt; The following objects are masked from &#39;package:stats&#39;:
  5. #&gt;
  6. #&gt; filter, lag
  7. #&gt; The following objects are masked from &#39;package:base&#39;:
  8. #&gt;
  9. #&gt; intersect, setdiff, setequal, union
  10. library(tidyr)
  11. temp &lt;- tibble::tribble(
  12. ~x1, ~x2, ~x3, ~x4, ~x5,
  13. 1, 2, 3, &#39;AA&#39;, &#39;BB&#39;,
  14. 2, 3, 4, &#39;AB&#39;, &#39;CC&#39;,
  15. 3, 4, 5, &#39;AC&#39;, &#39;DD&#39;,
  16. 4, 5, 6, &#39;AD&#39;, &#39;EE&#39;,
  17. 5, 6, 7, &#39;AE&#39;, &#39;FF&#39;,
  18. 6, 7, 8, &#39;AF&#39;, &#39;GG&#39;
  19. ) %&gt;% dplyr::mutate(dplyr::across(x4,as.character), dplyr::across(x5,as.character))
  20. temp_val &lt;- c(1,2,3)
  21. nam1 &lt;- c(&#39;px1&#39;,&#39;px2&#39;,&#39;px3&#39;)
  22. temp %&gt;%
  23. rowwise() %&gt;%
  24. transmute(newvars = list(c_across(where(is.numeric))*temp_val)) %&gt;%
  25. unnest_wider(newvars) %&gt;%
  26. setNames(nam1) %&gt;%
  27. bind_cols(temp, .)
  28. #&gt; # A tibble: 6 &#215; 8
  29. #&gt; x1 x2 x3 x4 x5 px1 px2 px3
  30. #&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  31. #&gt; 1 1 2 3 AA BB 1 4 9
  32. #&gt; 2 2 3 4 AB CC 2 6 12
  33. #&gt; 3 3 4 5 AC DD 3 8 15
  34. #&gt; 4 4 5 6 AD EE 4 10 18
  35. #&gt; 5 5 6 7 AE FF 5 12 21
  36. #&gt; 6 6 7 8 AF GG 6 14 24

<sup>Created on 2023-01-08 by the reprex package (v2.0.1)</sup>

huangapple
  • 本文由 发表于 2023年1月9日 01:27:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定