gpt4 book ai didi

r - 使用 gsub 或 sub 函数只获取字符串的一部分?

转载 作者:行者123 更新时间:2023-12-04 11:36:22 24 4
gpt4 key购买 nike

      Col
WBU-ARGU*06:03:04
WBU-ARDU*08:01:01
WBU-ARFU*11:03:05
WBU-ARFU*03:456

我有一列有 75 行变量,例如上面的 col。我不太确定如何使用 gsub 或 sub 以便在第一个冒号之后的整数之前起床。

预期输出:
      Col
WBU-ARGU*06:03
WBU-ARDU*08:01
WBU-ARFU*11:03
WBU-ARFU*03:456

我试过这个,但它似乎不起作用:
gsub("*..:","", df$col)

最佳答案

以下也可能对您有所帮助。

sub("([^:]*):([^:]*).*","\\1:\\2",df$dat)

输出如下。
> sub("([^:]*):([^:]*).*","\\1:\\2",df$dat)
[1] "WBU-ARGU*06:03" "WBU-ARDU*08:01" "WBU-ARFU*11:03" "WBU-ARFU*03:456b"

其中数据帧的输入如下。
dat <- c("WBU-ARGU*06:03:04","WBU-ARDU*08:01:01","WBU-ARFU*11:03:05","WBU-ARFU*03:456b")
df <- data.frame(dat)

说明: 以下仅作说明之用。
sub("      ##using sub for global subtitution function of R here.
([^:]*) ##By mentioning () we are keeping the matched values from vector's element into 1st place of memory(which we could use later), which is till next colon comes it will match everything.
: ##Mentioning letter colon(:) here.
([^:]*) ##By mentioning () making 2nd place in memory for matched values in vector's values which is till next colon comes it will match everything.
.*" ##Mentioning .* to match everything else now after 2nd colon comes in value.
,"\\1:\\2" ##Now mentioning the values of memory holds with whom we want to substitute the element values \\1 means 1st memory place \\2 is second memory place's value.
,df$dat) ##Mentioning df$dat dataframe's dat value.

关于r - 使用 gsub 或 sub 函数只获取字符串的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51539677/

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