Col
WBU-ARGU*06:03:04
WBU-ARDU*08:01:01
WBU-ARFU*11:03:05
WBU-ARFU*03:456
Run Code Online (Sandbox Code Playgroud)
我有一列有 75 行变量,例如上面的 col。我不太确定如何使用 gsub 或 sub 以便在第一个冒号之后的整数之前起床。
预期输出:
Col
WBU-ARGU*06:03
WBU-ARDU*08:01
WBU-ARFU*11:03
WBU-ARFU*03:456
Run Code Online (Sandbox Code Playgroud)
我试过这个,但它似乎不起作用:
gsub("*..:","", df$col)
Run Code Online (Sandbox Code Playgroud)
以下内容也可能对您有所帮助。
sub("([^:]*):([^:]*).*","\\1:\\2",df$dat)
Run Code Online (Sandbox Code Playgroud)
输出如下。
> sub("([^:]*):([^:]*).*","\\1:\\2",df$dat)
[1] "WBU-ARGU*06:03" "WBU-ARDU*08:01" "WBU-ARFU*11:03" "WBU-ARFU*03:456b"
Run Code Online (Sandbox Code Playgroud)
其中数据框的输入如下。
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)
Run Code Online (Sandbox Code Playgroud)
说明:以下内容仅用于说明目的。
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.
Run Code Online (Sandbox Code Playgroud)