根据条件添加行

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

Adding rows depending on condition

问题

我需要帮忙解决一个非常简单的问题:

假设我有这个数据框:

    data_new <- data.frame(section = c("1", "4", "5", "6"),
                           density = c("0.2", "0.7", "0.8", "0.2"))
    > data_new
      section density
    1       1     0.2
    2       4     0.7
    3       5     0.8
    4       6     0.2

我需要添加行,因为整个表是基于6个部分,但只有4个部分的数据。这意味着在这种情况下,我需要添加2行(部分2和3),密度为0,所以我有:

    > data_desired
      section density
    1       1     0.2
    2       4     0.7
    3       5     0.8
    4       6     0.2
    5       2       0
    6       3       0

关键是*0密度*行的组合可能会变化。在这种情况下,部分3和4是空的,但下一次可能没有部分具有0密度,或者我需要添加5个部分等等。这可能会有很大的变化,从一个部分具有数据到所有部分具有数据。

我确信有一种优雅的方法可以添加到我的管道中以添加我需要的行,这是特定情况下的。非常感谢您的帮助!
英文:

I need a little help with a very simple question:

Let's say I have this data frame:

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;))
&gt; data_new
  section density
1       1     0.2
2       4     0.7
3       5     0.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:

&gt; data_desired
  section density
1       1     0.2
2       4     0.7
3       5     0.8
4       6     0.2
5       2       0
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 的另一个选项:

library(dplyr)
# 创建零密度的数据框

n <- 6
data_zero <- data.frame(section = as.character(c(1:n)),
                       density = as.character(rep(0,n)))

data_new <- data.frame(section = c("1", "4", "5", "6"),
                       density = c("0.2", "0.7", "0.8", "0.2"))

rows_update(data_zero, data_new)
  section density
1       1     0.2
2       2       0
3       3       0
4       4     0.7
5       5     0.8
6       6     0.2

对于多列数据:

library(dplyr)
n <- 6
data_zero <- data.frame(section = as.character(c(1:n)),
                       density = as.character(rep(0,n)))

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"))

rows_update(data_zero, data_new[,c('section','density')]) %>%
  merge(data_new, all.x = TRUE)

  section density potatoes
1       1     0.2        a
2       2       0     <NA>
3       3       0     <NA>
4       4     0.7        n
5       5     0.8       ed
6       6     0.2        3
英文:

Another option using rows_update:

library(dplyr)
#create zero density dataframe

n&lt;-6
data_zero &lt;- data.frame(section = as.character(c(1:n)),
                       density = as.character(rep(0,n)))

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;))

rows_update(data_zero ,data_new)
  section density
1       1     0.2
2       2       0
3       3       0
4       4     0.7
5       5     0.8
6       6     0.2

For multiple columns:

library(dplyr)
n&lt;-6
data_zero &lt;- data.frame(section = as.character(c(1:n)),
                       density = as.character(rep(0,n)))


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;))

rows_update(data_zero ,data_new[,c(&#39;section&#39;,&#39;density&#39;)]) %&gt;%
  merge(data_new,all.x=T)

  section density potatoes
1       1     0.2        a
2       2       0     &lt;NA&gt;
3       3       0     &lt;NA&gt;
4       4     0.7        n
5       5     0.8       ed
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:

确定