gpt4 book ai didi

第一个和第二个之间的正则表达式 "_"

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

大家好,我有以下数据:

data
proprio_com_luz
proprio_sem_ola_acabo

我想创建两个新变量

data                   condition variable
proprio_com_luz com luz
proprio_sem_ola_acabo sem ola_acabo

什么正则表达式可以帮助我?

最佳答案

extract来自tidyr :

library(tidyr)

extract(df, data, c("condition", "variable"),
regex = "^[^_]+_([^_]+)_(.+)$", remove = FALSE)

或以 R 为基数:

pattern <- "^[^_]+_([^_]+)_(.+)$"

df$condition = sub(pattern, "\\1", df$data)
df$variable = sub(pattern, "\\2", df$data)

输出:

                   data condition  variable
1 proprio_com_luz com luz
2 proprio_sem_ola_acabo sem ola_acabo

数据:

df <- data.frame(data = c("proprio_com_luz",
"proprio_sem_ola_acabo"))

关于第一个和第二个之间的正则表达式 "_",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52747928/

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