Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何删除R中字符向量中字符串的公共部分?_R_Regex_String - Fatal编程技术网

如何删除R中字符向量中字符串的公共部分?

如何删除R中字符向量中字符串的公共部分?,r,regex,string,R,Regex,String,假设一个字符向量,如下所示 file1_p1_analysed_samples.txt file1_p1_raw_samples.txt f2_file2_p1_analysed_samples.txt f3_file3_p1_raw_samples.txt 期望输出: file1_p1_analysed file1_p1_raw file2_p1_analysed file3_p1_raw 我希望比较元素,并尽可能从开始和结束处删除字符串的部分,但保持它们的唯一性 以上只是一个例子。要拆下

假设一个字符向量,如下所示

file1_p1_analysed_samples.txt
file1_p1_raw_samples.txt
f2_file2_p1_analysed_samples.txt
f3_file3_p1_raw_samples.txt
期望输出:

file1_p1_analysed
file1_p1_raw
file2_p1_analysed
file3_p1_raw
我希望比较元素,并尽可能从开始和结束处删除字符串的部分,但保持它们的唯一性

以上只是一个例子。要拆下的零件并非所有元件都通用。我需要一个与上面示例中的字符串无关的通用解决方案

到目前为止,我已经能够扔掉所有元素共有的部分,只要分隔符和由此产生的分割部分具有相同的长度。这是函数

mf <- function(x,sep){
    xsplit = strsplit(x,split = sep)
    xdfm <- as.data.frame(do.call(rbind,xsplit))
    res <- list()
    for (i in 1:ncol(xdfm)){
        if (!all(xdfm[,i] == xdfm[1,i])){
            res[[length(res)+1]] <- as.character(xdfm[,i])
        }
    }
    res <- as.data.frame(do.call(rbind,res))
    res <- apply(res,2,function(x) paste(x,collapse="_"))
    return(res)
}
二,

如果生成的分割零件长度不同,则此操作无效

怎么样

files <- c("file1_p1_analysed_samples.txt", "file1_p1_raw_samples.txt", "f2_file2_p1_analysed_samples.txt", "f3_file3_p1_raw_samples.txt")
new_files <- gsub('_samples\\.txt', '', files)
new_files
这将从字符串中删除
\u samples.txt
部分。

如何

files <- c("file1_p1_analysed_samples.txt", "file1_p1_raw_samples.txt", "f2_file2_p1_analysed_samples.txt", "f3_file3_p1_raw_samples.txt")
new_files <- gsub('_samples\\.txt', '', files)
new_files
这将从字符串中删除
\u samples.txt
部分。

为什么不:

strings <- c("file1_p1_analysed_samples.txt",
"file1_p1_raw_samples.txt",
"f2_file2_p1_analysed_samples.txt",
"f3_file3_p1_raw_samples.txt")

sapply(strings, function(x) {
  pattern <- ".*(file[0-9].*)_samples\\.txt"
  gsub(x, pattern = pattern, replacement = "\\1")
})
strings为什么不:

strings <- c("file1_p1_analysed_samples.txt",
"file1_p1_raw_samples.txt",
"f2_file2_p1_analysed_samples.txt",
"f3_file3_p1_raw_samples.txt")

sapply(strings, function(x) {
  pattern <- ".*(file[0-9].*)_samples\\.txt"
  gsub(x, pattern = pattern, replacement = "\\1")
})

strings如果要删除的部分在所有元素中都是相同的,这很容易:
gsub(“\u samples.txt”,”,[your vector])
。不,它不一样。@Veera为什么要删除
f2
f3
?他们不一样。@PoGibas是的。但是在删除它们之后,结果字符串仍然是唯一的。只有当字符串不再唯一时,我才想停止删除。@ulfelder:您需要转义圆点。否则它只是另一个字符(可能是一个点,但也可能是其他任何字符)。如果要删除的部分在所有元素中都是相同的,这很容易:
gsub(“\u samples.txt”,”,[your vector])
。不,它不一样。@Veera为什么要删除
f2
f3
?他们不一样。@PoGibas是的。但是在删除它们之后,结果字符串仍然是唯一的。只有当字符串不再唯一时,我才想停止删除。@ulfelder:您需要转义圆点。否则它只是另一个字符(可能是一个点,但也可能是其他任何字符)。哦,@Jan的答案要好得多。也许可以改变他的模式:
'.*(文件[0-9].\\\.txt'
,别忘了用
\\1
反向引用,@Jan的答案要好得多。也许可以用以下内容更改他的模式:
'.*(文件[0-9].\\\.txt'
,不要忘记使用
\\1
进行反向引用不,这不是我想要的。我需要一个解决方案,可以自动确定向量中字符串的唯一和非唯一部分,并只删除非唯一部分。不,这不是我想要的。我需要一个解决方案,可以自动确定向量中字符串的唯一部分和非唯一部分,并仅删除非唯一部分。
strings <- c("file1_p1_analysed_samples.txt",
"file1_p1_raw_samples.txt",
"f2_file2_p1_analysed_samples.txt",
"f3_file3_p1_raw_samples.txt")

sapply(strings, function(x) {
  pattern <- ".*(file[0-9].*)_samples\\.txt"
  gsub(x, pattern = pattern, replacement = "\\1")
})