根据条件添加行

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

Adding rows depending on condition

问题

  1. 我需要帮忙解决一个非常简单的问题:
  2. 假设我有这个数据框:
  3. data_new <- data.frame(section = c("1", "4", "5", "6"),
  4. density = c("0.2", "0.7", "0.8", "0.2"))
  5. > data_new
  6. section density
  7. 1 1 0.2
  8. 2 4 0.7
  9. 3 5 0.8
  10. 4 6 0.2
  11. 我需要添加行,因为整个表是基于6个部分,但只有4个部分的数据。这意味着在这种情况下,我需要添加2行(部分23),密度为0,所以我有:
  12. > data_desired
  13. section density
  14. 1 1 0.2
  15. 2 4 0.7
  16. 3 5 0.8
  17. 4 6 0.2
  18. 5 2 0
  19. 6 3 0
  20. 关键是*0密度*行的组合可能会变化。在这种情况下,部分34是空的,但下一次可能没有部分具有0密度,或者我需要添加5个部分等等。这可能会有很大的变化,从一个部分具有数据到所有部分具有数据。
  21. 我确信有一种优雅的方法可以添加到我的管道中以添加我需要的行,这是特定情况下的。非常感谢您的帮助!
英文:

I need a little help with a very simple question:

Let's say I have this data frame:

  1. data_new &lt;- data.frame(section = c(&quot;1&quot;, &quot;4&quot;, &quot;5&quot;,&quot;6&quot;),
  2. density = c(&quot;0.2&quot;, &quot;0.7&quot;, &quot;0.8&quot;, &quot;0.2&quot;))
  3. &gt; data_new
  4. section density
  5. 1 1 0.2
  6. 2 4 0.7
  7. 3 5 0.8
  8. 4 6 0.2

I need to add rows because the full table is based on 6 sections, but only have data on 4. This means that in this case I have to add 2 rows (sections 2 and 3) with density 0 so I have:

  1. &gt; data_desired
  2. section density
  3. 1 1 0.2
  4. 2 4 0.7
  5. 3 5 0.8
  6. 4 6 0.2
  7. 5 2 0
  8. 6 3 0

The point is that the combination of 0 density rows may vary. In this case sections 3 and 4 were empty, but next time it may be that no section has density 0 or that I have to add 5 sections, etc. It can vary a lot, from 1 section with data to all sections with data.

I'm sure there is an elegant way to add to my pipe to ad the rows I need and that is case specific. Thanks a lot for your help!!

答案1

得分: 1

使用 rows_update 的另一个选项:

  1. library(dplyr)
  2. # 创建零密度的数据框
  3. n <- 6
  4. data_zero <- data.frame(section = as.character(c(1:n)),
  5. density = as.character(rep(0,n)))
  6. data_new <- data.frame(section = c("1", "4", "5", "6"),
  7. density = c("0.2", "0.7", "0.8", "0.2"))
  8. rows_update(data_zero, data_new)
  9. section density
  10. 1 1 0.2
  11. 2 2 0
  12. 3 3 0
  13. 4 4 0.7
  14. 5 5 0.8
  15. 6 6 0.2

对于多列数据:

  1. library(dplyr)
  2. n <- 6
  3. data_zero <- data.frame(section = as.character(c(1:n)),
  4. density = as.character(rep(0,n)))
  5. data_new <- data.frame(section = c("1", "4", "5", "6"), density = c("0.2", "0.7", "0.8", "0.2"), potatoes = c("a", "n", "ed", "3"))
  6. rows_update(data_zero, data_new[,c('section','density')]) %>%
  7. merge(data_new, all.x = TRUE)
  8. section density potatoes
  9. 1 1 0.2 a
  10. 2 2 0 <NA>
  11. 3 3 0 <NA>
  12. 4 4 0.7 n
  13. 5 5 0.8 ed
  14. 6 6 0.2 3
英文:

Another option using rows_update:

  1. library(dplyr)
  2. #create zero density dataframe
  3. n&lt;-6
  4. data_zero &lt;- data.frame(section = as.character(c(1:n)),
  5. density = as.character(rep(0,n)))
  6. data_new &lt;- data.frame(section = c(&quot;1&quot;, &quot;4&quot;, &quot;5&quot;,&quot;6&quot;),
  7. density = c(&quot;0.2&quot;, &quot;0.7&quot;, &quot;0.8&quot;, &quot;0.2&quot;))
  8. rows_update(data_zero ,data_new)
  9. section density
  10. 1 1 0.2
  11. 2 2 0
  12. 3 3 0
  13. 4 4 0.7
  14. 5 5 0.8
  15. 6 6 0.2

For multiple columns:

  1. library(dplyr)
  2. n&lt;-6
  3. data_zero &lt;- data.frame(section = as.character(c(1:n)),
  4. density = as.character(rep(0,n)))
  5. data_new &lt;- data.frame(section = c(&quot;1&quot;, &quot;4&quot;, &quot;5&quot;,&quot;6&quot;), density = c(&quot;0.2&quot;, &quot;0.7&quot;, &quot;0.8&quot;, &quot;0.2&quot;), potatoes = c(&quot;a&quot;,&quot;n&quot;,&quot;ed&quot;,&quot;3&quot;))
  6. rows_update(data_zero ,data_new[,c(&#39;section&#39;,&#39;density&#39;)]) %&gt;%
  7. merge(data_new,all.x=T)
  8. section density potatoes
  9. 1 1 0.2 a
  10. 2 2 0 &lt;NA&gt;
  11. 3 3 0 &lt;NA&gt;
  12. 4 4 0.7 n
  13. 5 5 0.8 ed
  14. 6 6 0.2 3

huangapple
  • 本文由 发表于 2023年2月10日 05:27:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404577.html
匿名

发表评论

匿名网友

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

确定