% separate(id, into = c("dad2", "mom2"), sep = "/", fill = "right",-6ren">
gpt4 book ai didi

r - 如果条件在 R 中匹配,则将一列拆分为两个现有列

转载 作者:行者123 更新时间:2023-12-05 09:02:02 26 4
gpt4 key购买 nike

我有一个这样的 df:

df <- data.frame(id=c("j1", "j2", "j3/j9", "j5", "j2/j8", "j3/j4"), dad=c("j10", "j11", "", "j13", "", ""), mom=c("k2", "k4", "", "k6", "", ""))

我试图只拆分那些在“id”列中包含斜杠“/”的单元格。我想在现有列“爸爸”和“妈妈”中获取拆分字符串。所需的输出将是这样的:

df2 <- data.frame(id=c("j1", "j2", "j3/j9", "j5", "j2/j8", "j3/j4"), dad=c("j10", "j11", "j3", "j13", "j2", "j3"), mom=c("k2", "k4", "j9", "k6", "j8", "j4"))

我正在尝试这段代码:

df3 <- tidyr::separate(data = df, col = "id", into = c("dad", "mom"), sep = "/")

但是将整个“id”列拆分为两个新列。知道如何解决这个问题吗?

最佳答案

这是在 separateing 之后使用 coalesce 的一种方法 - 将空白 ("") 转换为 NA (na_if), separate the 'id' into 'dad2', 'mom2', columns, loop across the 'dad', 'mom ' 列和 合并 与相应的 'dad2', 'mom2' 列

library(dplyr)
library(tidyr)
library(stringr)
df %>%
na_if("") %>%
separate(id, into = c("dad2", "mom2"), sep = "/", fill = "right",
remove = FALSE) %>%
mutate(across(dad:mom, ~ coalesce(.x, get(str_c(cur_column(),
2)))), .keep = "unused")

-输出

     id dad mom
1 j1 j10 k2
2 j2 j11 k4
3 j3/j9 j3 j9
4 j5 j13 k6
5 j2/j8 j2 j8
6 j3/j4 j3 j4

或者更有用的是 across2 from dplyover

library(dplyover)
df %>%
na_if("") %>%
separate(id, into = c("dad2", "mom2"), sep = "/", fill = "right",
remove = FALSE) %>%
mutate(across2(dad:mom, dad2:mom2, coalesce, .names = "{xcol}")) %>%
select(names(df))

关于r - 如果条件在 R 中匹配,则将一列拆分为两个现有列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72189661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com