在R中,将一些列的值合并后,向数据框添加一列。

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

Add a column to data frame resulting from the merging of some values from other columns with R

问题

我有一个包含许多列的数据框,其中包括以下内容:

  1. COLUMN_1 COLUMN_2 COLUMN_3
  2. red blue green
  3. none blue none
  4. red none green
  5. red none none
  6. none none none

我想基于一个布尔值添加第四列COLORS,对于COLUMN_1的每个值为'red',COLUMN_2的每个值为'blue',或者COLUMN_3的每个值为'green',都会给出'True',如果COLUMN_1,COLUMN_2和COLUMN_3的值始终为'none',则给出'False'。就像这样:

  1. COLUMN_1 COLUMN_2 COLUMN_3 COLORS
  2. red blue green True
  3. none blue none True
  4. red none green True
  5. red none none True
  6. none none none False

一个不带布尔值的列也可以:

  1. COLUMN_1 COLUMN_2 COLUMN_3 COLORS
  2. red blue green Color
  3. none blue none Color
  4. red none green Color
  5. red none none Color
  6. none none none No color
英文:

I have a data frame with many columns, among which I have the following:

  1. COLUMN_1 COLUMN_2 COLUMN_3
  2. red blue green
  3. none blue none
  4. red none green
  5. red none none
  6. none none none

I'd like to add a fourth column called COLORS based on a boolean which gives 'True' for every value 'red' of COLUMN_1, 'blue' of COLUMN_2, or 'green' of COLUMN_3, and gives 'False' if the value of COLUMN_1, COLUMN_2 and COLUMN_3 is always 'none'. Like this:

  1. COLUMN_1 COLUMN_2 COLUMN_3 COLORS
  2. red blue green True
  3. none blue none True
  4. red none green True
  5. red none none True
  6. none none none False

A column without a boolean would also work:

  1. COLUMN_1 COLUMN_2 COLUMN_3 COLORS
  2. red blue green Color
  3. none blue none Color
  4. red none green Color
  5. red none none Color
  6. none none none No color

答案1

得分: 2

Here is the translated code without the translation of code-related terms:

  1. library(dplyr)
  2. df %>%
  3. mutate(
  4. COLORS = case_when(
  5. if_all(contains("COLUMN"), ~ .x == "none") ~ "无颜色",
  6. TRUE ~ "有颜色"
  7. ))

And the table section:

  1. # A tibble: 5 × 4
  2. COLUMN_1 COLUMN_2 COLUMN_3 COLORS
  3. <chr> <chr> <chr> <chr>
  4. 1 red blue green 有颜色
  5. 2 none blue none 有颜色
  6. 3 red none green 有颜色
  7. 4 red none none 有颜色
  8. 5 none none none 无颜色
英文:
  1. library(dplyr)
  2. df %&gt;%
  3. mutate(
  4. COLORS = case_when(
  5. if_all(contains(&quot;COLUMN&quot;), ~ .x == &quot;none&quot;) ~ &quot;No color&quot;,
  6. TRUE ~ &quot;Color&quot;
  7. ))
  8. # A tibble: 5 &#215; 4
  9. COLUMN_1 COLUMN_2 COLUMN_3 COLORS
  10. &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  11. 1 red blue green Color
  12. 2 none blue none Color
  13. 3 red none green Color
  14. 4 red none none Color
  15. 5 none none none No color

答案2

得分: 2

一种选择可能是:

  1. df %>%
  2. rowwise() %>%
  3. mutate(COLORS = any(across(everything()) == c("red", "blue", "green")))
  4. COLUMN_1 COLUMN_2 COLUMN_3 COLORS
  5. <chr> <chr> <chr> <lgl>
  6. 1 red blue green TRUE
  7. 2 none blue none TRUE
  8. 3 red none green TRUE
  9. 4 red none none TRUE
  10. 5 none none none FALSE

如果某个列的颜色与预期不同,它将返回 FALSE:

  1. COLUMN_1 COLUMN_2 COLUMN_3 COLORS
  2. <chr> <chr> <chr> <lgl>
  3. 1 red blue green TRUE
  4. 2 none blue none TRUE
  5. 3 red none green TRUE
  6. 4 red none none TRUE
  7. 5 blue none none FALSE
英文:

One option could be:

  1. df %&gt;%
  2. rowwise() %&gt;%
  3. mutate(COLORS = any(across(everything()) == c(&quot;red&quot;, &quot;blue&quot;, &quot;green&quot;)))
  4. COLUMN_1 COLUMN_2 COLUMN_3 COLORS
  5. &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;lgl&gt;
  6. 1 red blue green TRUE
  7. 2 none blue none TRUE
  8. 3 red none green TRUE
  9. 4 red none none TRUE
  10. 5 none none none FALSE

If a given column would have a different color as expected, the it would return FALSE:

  1. COLUMN_1 COLUMN_2 COLUMN_3 COLORS
  2. &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;lgl&gt;
  3. 1 red blue green TRUE
  4. 2 none blue none TRUE
  5. 3 red none green TRUE
  6. 4 red none none TRUE
  7. 5 blue none none FALSE

答案3

得分: 0

以下是您要翻译的内容:

"这并不是一个高效的答案,而是一个替代的答案

  1. df2 &lt;- df %&gt;%
  2. mutate(across(c(COLUMN_1,COLUMN_2,COLUMN_3), ~ ifelse(.x==&#39;none&#39;, 1, 0), .names = &#39;n_{.col}&#39;),
  3. new=rowSums(across(c(n_COLUMN_1,n_COLUMN_2,n_COLUMN_3))),
  4. COLORS=ifelse(new==3,&#39;no color&#39;, &#39;color&#39;)) %&gt;%
  5. select(-contains(&#39;n_c&#39;), -new)

<sup>创建于2023-06-08,使用 reprex v2.0.2</sup>

  1. # A tibble: 5 &#215; 4
  2. COLUMN_1 COLUMN_2 COLUMN_3 COLORS
  3. &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  4. 1 红色 蓝色 绿色 有颜色
  5. 2 蓝色 有颜色
  6. 3 红色 绿色 有颜色
  7. 4 红色 有颜色
  8. 5 无颜色
英文:

It is not an efficient answer but an alternate

  1. df2 &lt;- df %&gt;%
  2. mutate(across(c(COLUMN_1,COLUMN_2,COLUMN_3), ~ ifelse(.x==&#39;none&#39;, 1, 0), .names = &#39;n_{.col}&#39;),
  3. new=rowSums(across(c(n_COLUMN_1,n_COLUMN_2,n_COLUMN_3))),
  4. COLORS=ifelse(new==3,&#39;no color&#39;, &#39;color&#39;)) %&gt;%
  5. select(-contains(&#39;n_c&#39;), -new)

<sup>Created on 2023-06-08 with reprex v2.0.2</sup>

  1. # A tibble: 5 &#215; 4
  2. COLUMN_1 COLUMN_2 COLUMN_3 COLORS
  3. &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  4. 1 red blue green color
  5. 2 none blue none color
  6. 3 red none green color
  7. 4 red none none color
  8. 5 none none none no color

答案4

得分: 0

我们可以尝试使用 rowSums + col,如下所示:

  1. transform(
  2. df,
  3. COLORS = rowSums(df == c("red", "blue", "green")[col(df)]) > 0
  4. )

这将得到以下结果:

  1. COLUMN_1 COLUMN_2 COLUMN_3 COLORS
  2. 1 red blue green TRUE
  3. 2 none blue none TRUE
  4. 3 red none green TRUE
  5. 4 red none none TRUE
  6. 5 none none none FALSE
英文:

We can try rowSums + col like below

  1. transform(
  2. df,
  3. COLORS = rowSums(df == c(&quot;red&quot;, &quot;blue&quot;, &quot;green&quot;)[col(df)]) &gt; 0
  4. )

which gives

  1. COLUMN_1 COLUMN_2 COLUMN_3 COLORS
  2. 1 red blue green TRUE
  3. 2 none blue none TRUE
  4. 3 red none green TRUE
  5. 4 red none none TRUE
  6. 5 none none none FALSE

答案5

得分: 0

  1. library(dplyr)
  2. df |&gt;
  3. mutate(COLORS = rowSums(pick(starts_with(&quot;COLUMN&quot;)) != &quot;none&quot;) &gt; 0)

注意:如果存在颜色“yellow”,则返回TRUE

英文:
  1. library(dplyr)
  2. df |&gt;
  3. mutate(COLORS = rowSums(pick(starts_with(&quot;COLUMN&quot;)) != &quot;none&quot;) &gt; 0)

Note: this will return TRUE if there is a color "yellow".

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

发表评论

匿名网友

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

确定