英文:
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_rows
和mutate
if_else
,但我完全被难住了。任何帮助将不胜感激!
英文:
I have a dataset like this:
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
But I need to create new rows so that the non-zero values of pairs of names have their own row like this:
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
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 |>
as_tibble() |>
pivot_longer(
cols = starts_with("v"),
names_to = 'var',
values_to = 'value'
) |>
filter(value != 0) |>
mutate(
var2 = var # dummy variable to trick `pivot_wider` to do what you want
) |>
pivot_wider(
names_from = var,
values_from = value,
values_fill = "0" # 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.
) |>
select(-var2) # remove dummy variable
英文:
library(tidyverse)
df1 |>
as_tibble() |>
pivot_longer(
cols = starts_with("v"),
names_to = 'var',
values_to = 'value'
) |>
filter(value != 0) |>
mutate(
var2 = var # dummy variable to trick `pivot_wider` to do what you want
) |>
pivot_wider(
names_from = var,
values_from = value,
values_fill = "0" # 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.
) |>
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("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
Data:
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)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论