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/