将包含多个非零值的数据行分开,使新行每行只包含一个非零值。

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

Separating rows of data containing multiple non-zeros, so new rows contain one non-zero value each

问题

I understand that you want a Chinese translation of the code and its description. Here it is:

我明白您想要代码和描述的中文翻译。以下是翻译:

我有一个类似这样的数据集:

df1 <- cbind(c("a","b"),c("c","c"),c(0,8),c(4,0),c(5,0),c(0,12))
colnames(df1) <- c("name1","name2","v1","v2","v3","v4")

name1 name2 v1 v2 v3 v4
    a     c  0  4  5  0
    b     c  8  0  0 12

但我需要创建新的行,以便名称对的非零值有自己的行,就像这样:

df2 <- cbind(c("a","a","b","b"),c("c","c","c","c"),c(0,0,8,0),c(4,0,0,0),c(0,5,0,0),c(0,0,0,12))
colnames(df2) <- c("name1","name2","v1","v2","v3","v4")

name1 name2 v1 v2 v3 v4
    a     c  0  4  0  0
    a     c  0  0  5  0
    b     c  8  0  0  0
    b     c  0  0  0 12

这是我的第一个问题,所以希望这足够详细。我尝试过separate_rowsmutate if_else,但我完全被难住了。任何帮助将不胜感激!

英文:

I have a dataset like this:

df1 &lt;- cbind(c(&quot;a&quot;,&quot;b&quot;),c(&quot;c&quot;,&quot;c&quot;),c(0,8),c(4,0),c(5,0),c(0,12))
colnames(df1) &lt;- c(&quot;name1&quot;,&quot;name2&quot;,&quot;v1&quot;,&quot;v2&quot;,&quot;v3&quot;,&quot;v4&quot;)

name1 name2 v1 v2 v3 v4
    a     c  0  4  5  0
    b     c  8  0  0 12

But I need to create new rows so that the non-zero values of pairs of names have their own row like this:

df2 &lt;- cbind(c(&quot;a&quot;,&quot;a&quot;,&quot;b&quot;,&quot;b&quot;),c(&quot;c&quot;,&quot;c&quot;,&quot;c&quot;,&quot;c&quot;),c(0,0,8,0),c(4,0,0,0),c(0,5,0,0),c(0,0,0,12))
colnames(df2) &lt;- c(&quot;name1&quot;,&quot;name2&quot;,&quot;v1&quot;,&quot;v2&quot;,&quot;v3&quot;,&quot;v4&quot;)

name1 name2 v1 v2 v3 v4
    a     c  0  4  0  0
    a     c  0  0  5  0
    b     c  8  0  0  0
    b     c  0  0  0 12

This is my first question so I hope that's enough detail. I've tried separate_rows, and mutate if_else, but I'm completely stumped. Any help would be greatly appreciated!

答案1

得分: 2

library(tidyverse)

df1 |&gt;
  as_tibble() |&gt;
  pivot_longer(
    cols = starts_with(&quot;v&quot;),
    names_to = &#39;var&#39;,
    values_to = &#39;value&#39;
  ) |&gt;
  filter(value != 0) |&gt;
  mutate(
    var2 = var  # dummy variable to trick `pivot_wider` to do what you want
  ) |&gt;
  pivot_wider(
    names_from = var,
    values_from = value,
    values_fill = &quot;0&quot;  # values are forced to be characters due to how data is entered/saved.  Might need to change to 0 (without the quotes) in your real data.
  ) |&gt;
select(-var2) # remove dummy variable
英文:
library(tidyverse)

df1 |&gt;
  as_tibble() |&gt;
  pivot_longer(
    cols = starts_with(&quot;v&quot;),
    names_to = &#39;var&#39;,
    values_to = &#39;value&#39;
  ) |&gt;
  filter(value != 0) |&gt;
  mutate(
    var2 = var  # dummy variable to trick `pivot_wider` to do what you want
  ) |&gt;
  pivot_wider(
    names_from = var,
    values_from = value,
    values_fill = &quot;0&quot;  # values are forced to be characters due to how data is entered/saved.  Might need to change to 0 (without the quotes) in your real data.
  ) |&gt;
select(-var2) # remove dummy variable

答案2

得分: 1

# 使用 data.table 的 melt 和 dcast 函数:
library(data.table)

dcast(
  melt(setDT(df1), c("name1", "name2"))[value != 0],
  name1 + name2 + value ~ variable, fill = 0
)[, value := NULL][]
#>    name1 name2 v1 v2 v3 v4
#> 1:     a     c  0  4  0  0
#> 2:     a     c  0  0  5  0
#> 3:     b     c  8  0  0  0
#> 4:     b     c  0  0  0 12

数据:

df1 <- data.frame(
  name1 = c("a","b"),
  name2 = c("c","c"),
  v1 = c(0,8),
  v2 = c(4,0),
  v3 = c(5,0),
  v4 = c(0,12)
)
英文:

A data.table melt and dcast:

library(data.table)

dcast(
  melt(setDT(df1), c(&quot;name1&quot;, &quot;name2&quot;))[value != 0],
  name1 + name2 + value ~ variable, fill = 0,
)[, value := NULL][]
#&gt;    name1 name2 v1 v2 v3 v4
#&gt; 1:     a     c  0  4  0  0
#&gt; 2:     a     c  0  0  5  0
#&gt; 3:     b     c  8  0  0  0
#&gt; 4:     b     c  0  0  0 12

Data:

df1 &lt;- data.frame(
  name1 = c(&quot;a&quot;,&quot;b&quot;),
  name2 = c(&quot;c&quot;,&quot;c&quot;),
  v1 = c(0,8),
  v2 = c(4,0),
  v3 = c(5,0),
  v4 = c(0,12)
)

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

发表评论

匿名网友

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

确定