如何使用R中的Pipe函数将一行除以另一行

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

How to Divide a Row with Another Row by Pipe Function in R

问题

我正在尝试通过将两行的值相除来创建一个名为**'DB Q'**的新列,但我无法做到。我以为可以使用rowwise()函数来实现,但它不起作用,我不确定是因为在执行时同时保留其他列,还是语法只是简单错误。

附上我编写的代码,理想情况下,我想创建更多的行,所以最好通过管道传递。有人能帮助我吗?

  1. library(readxl)
  2. library(dplyr)
  3. Pd_Clean <- Pd_Clean %>%
  4. rowwise(SKU) %>%
  5. mutate(`DB Q` = `BH Q` / SI)

我正在处理的数据框如下所示:

  1. structure(list(SKU = c("XYZ_20645", "XYZ_20645"), `Key Figures` = c("BH Q",
  2. "SI"), `Jan-19` = c(NA, 1), `Feb-19` = c(NA, 1), `Mar-19` = c(NA,
  3. 1), `Apr-19` = c(NA, 1), `May-19` = c(NA, 1), `Jun-19` = c(NA,
  4. 1), `Jul-19` = c(NA, 1), `Aug-19` = c(NA, 1), `Sep-19` = c(NA,
  5. 1), `Oct-19` = c(NA, 1), `Nov-19` = c(NA, 1), `Dec-19` = c(13,
  6. 1), `Jan-20` = c(NA, 1), `Feb-20` = c(14, 1), `Mar-20` = c(NA,
  7. 1), `Apr-20` = c(NA, 1), `May-20` = c(NA, 1), `Jun-20` = c(NA,
  8. 1), `Jul-20` = c(NA, 1), `Aug-20` = c(NA, 1.419196), `Sep-20` = c(NA,
  9. 0.853283), `Oct-20` = c(NA, 0.799518), `Nov-20` = c(24, 0.709845
  10. ), `Dec-20` = c(NA, 0.891075), `Jan-21` = c(NA, 1.113528), `Feb-21` = c(NA,
  11. 0.593329), `Mar-21` = c(14, 0.914788), `Apr-21` = c(NA, 1.111516
  12. ), `May-21` = c(NA, 1.287797), `Jun-21` = c(110, 1.254029), `Jul-21` = c(NA,
  13. 1.052091), `Aug-21` = c(35, 1.491094), `Sep-21` = c(NA, 0.893486
  14. ), `Oct-21` = c(59, 0.831871), `Nov-21` = c(79, 0.738738), `Dec-21` = c(316,
  15. 0.910051)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
  16. -2L))

预期输出如下:在黄色突出显示的行是需要创建的新行,蓝色突出显示的单元格是数学公式。

如何使用R中的Pipe函数将一行除以另一行

英文:

I am trying to create a new column named 'DB Q' by dividing the value of the two rows and I am not able to do it. I thought I could do it with rowwise() function but it is not working, I am not sure if I am complicating this by keeping the other column as well while performing or the syntax is just simply wrong.

Attached is the code which I wrote, ideally I want to create a lot more rows so passing it through pipe is preferred. Can someone help me out.

  1. library(readxl)
  2. library(dplyr)
  3. Pd_Clean &lt;- Pd_Clean %&gt;%
  4. rowwise(SKU) %&gt;%
  5. mutate(`DB Q`= `BH Q`/SI)

The data frame I am working with is given below:

  1. structure(list(SKU = c(&quot;XYZ_20645&quot;, &quot;XYZ_20645&quot;), `Key Figures` = c(&quot;BH Q&quot;,
  2. &quot;SI&quot;), `Jan-19` = c(NA, 1), `Feb-19` = c(NA, 1), `Mar-19` = c(NA,
  3. 1), `Apr-19` = c(NA, 1), `May-19` = c(NA, 1), `Jun-19` = c(NA,
  4. 1), `Jul-19` = c(NA, 1), `Aug-19` = c(NA, 1), `Sep-19` = c(NA,
  5. 1), `Oct-19` = c(NA, 1), `Nov-19` = c(NA, 1), `Dec-19` = c(13,
  6. 1), `Jan-20` = c(NA, 1), `Feb-20` = c(14, 1), `Mar-20` = c(NA,
  7. 1), `Apr-20` = c(NA, 1), `May-20` = c(NA, 1), `Jun-20` = c(NA,
  8. 1), `Jul-20` = c(NA, 1), `Aug-20` = c(NA, 1.419196), `Sep-20` = c(NA,
  9. 0.853283), `Oct-20` = c(NA, 0.799518), `Nov-20` = c(24, 0.709845
  10. ), `Dec-20` = c(NA, 0.891075), `Jan-21` = c(NA, 1.113528), `Feb-21` = c(NA,
  11. 0.593329), `Mar-21` = c(14, 0.914788), `Apr-21` = c(NA, 1.111516
  12. ), `May-21` = c(NA, 1.287797), `Jun-21` = c(110, 1.254029), `Jul-21` = c(NA,
  13. 1.052091), `Aug-21` = c(35, 1.491094), `Sep-21` = c(NA, 0.893486
  14. ), `Oct-21` = c(59, 0.831871), `Nov-21` = c(79, 0.738738), `Dec-21` = c(316,
  15. 0.910051)), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA,
  16. -2L))

The Expected output is given below: The row highlighted in yellow is the new row which needs to be created and the cell highlighted in blue is the mathematical formulae.

如何使用R中的Pipe函数将一行除以另一行

答案1

得分: 1

以下是翻译好的代码部分:

第一段代码:

  1. Pd_Clean[3, -c(1:2)] <- lapply(Pd_Clean[-c(1:2)],
  2. function(x) ifelse(is.na(x[1]), 0,
  3. round(x[1] / x[2])))
  4. Pd_Clean[3, 1:2] <- c(Pd_Clean[1,1], "DB Q")

第二段代码:

  1. Pd_Clean[3, -c(1:2)] <- ifelse(is.na(Pd_Clean[3, -c(1:2)]), 0,
  2. Pd_Clean[1, -c(1:2)] / Pd_Clean[2, -c(1:2)])

输出部分:

  1. Pd_Clean[22:38] # 图中的列
  2. # `Aug-20` `Sep-20` `Oct-20` `Nov-20` `Dec-20` `Jan-21` `Feb-21` `Mar-21` `Apr-21` `May-21` `Jun-21` `Jul-21` `Aug-21` `Sep-21` `Oct-21` `Nov-21` `Dec-21`
  3. # <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
  4. # 1 NA NA NA 24 NA NA NA 14 NA NA 110 NA 35 NA 59 79 316
  5. # 2 1.42 0.853 0.800 0.710 0.891 1.11 0.593 0.915 1.11 1.29 1.25 1.05 1.49 0.893 0.832 0.739 0.910
  6. # 3 0 0 0 34 0 0 0 15 0 0 88 0 23 0 71 107 347

希望这有所帮助。如果有任何其他问题,请随时提出。

英文:

One base R approach is to use simple indexing to create a new column then lapply to perform the function across columns:

  1. Pd_Clean[3, -c(1:2)] &lt;- lapply(Pd_Clean[-c(1:2)],
  2. function(x) ifelse(is.na(x[1]), 0,
  3. round(x[1] / x[2])))
  4. Pd_Clean[3, 1:2] &lt;- c(Pd_Clean[1,1], &quot;DB Q&quot;)

You could also do the first step without lapply(), but not as clean in my option:

  1. Pd_Clean[3, -c(1:2)] &lt;- ifelse(is.na(Pd_Clean[3, -c(1:2)]), 0,
  2. Pd_Clean[1, -c(1:2)] / Pd_Clean[2, -c(1:2)])

Output:

  1. Pd_Clean[22:38] # columns in image
  2. # `Aug-20` `Sep-20` `Oct-20` `Nov-20` `Dec-20` `Jan-21` `Feb-21` `Mar-21` `Apr-21` `May-21` `Jun-21` `Jul-21` `Aug-21` `Sep-21` `Oct-21` `Nov-21` `Dec-21`
  3. # &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  4. # 1 NA NA NA 24 NA NA NA 14 NA NA 110 NA 35 NA 59 79 316
  5. # 2 1.42 0.853 0.800 0.710 0.891 1.11 0.593 0.915 1.11 1.29 1.25 1.05 1.49 0.893 0.832 0.739 0.910
  6. # 3 0 0 0 34 0 0 0 15 0 0 88 0 23 0 71 107 347

It's a little unclear what you mean by "I want to create a lot more rows" so if you provide more robust example data I am happy to update.

答案2

得分: -2

我看不到你附上的图片。这是你想要的输出吗?我写了一段相当丑陋的代码,我相信有更好的方法,也许可以使用 c_across

  1. Pd_Clean |>
  2. t() |>
  3. data.frame() |>
  4. mutate(across(everything(), as.numeric)) |>
  5. mutate(answer = X1/X2) |>
  6. t() |>
  7. data.frame() |>
  8. suppressWarnings()
英文:

I can't see the image you attached. Is this the output you wanted? I wrote a pretty ugly code and I'm sure there is a better way, maybe with c_across:

  1. Pd_Clean |&gt;
  2. t() |&gt;
  3. data.frame() |&gt;
  4. mutate(across(everything(),as.numeric)) |&gt;
  5. mutate(answer = X1/X2) |&gt;
  6. t() |&gt;
  7. data.frame() |&gt;
  8. suppressWarnings()

如何使用R中的Pipe函数将一行除以另一行

huangapple
  • 本文由 发表于 2023年6月15日 20:40:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482582.html
匿名

发表评论

匿名网友

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

确定