如何在R中的另一个字符串向量中grep一个字符串向量

时间:2019-04-08 14:43:39

标签: r

假设我有两个字符串向量:

s1<-c("ab", "cd-e", "hij")
s2<-c("0ab12345", "xyzcd-e", "uvwxyz132", "13216469", "%qrst012")

我想要的是找到s1中出现的s2中的项目,因此上面的示例将给出输出

"ab", "cd-e"

3 个答案:

答案 0 :(得分:3)

org.springframework.web.bind.annotation与指示的功能一起使用。不使用任何软件包。

Filter

答案 1 :(得分:2)

对于grepl中的每个值,我们可以s2覆盖s1,然后选择是否有TRUE

s1[colSums(sapply(s1, grepl, s2)) > 0]
#[1] "ab"   "cd-e"

答案 2 :(得分:0)

这是一种方法

public static void setData(String dataValues, String path) {
        Workbook workbook;
        try {
            File excelExportFile = new File(path);
            workbook = WorkbookFactory.create(excelExportFile);
            Sheet sheet = workbook.getSheetAt(0);
            int nextRow = sheet.getLastRowNum()+1;
            Row row = sheet.getRow(nextRow);
            row = sheet.createRow(nextRow);
            int cellnum=0;
            List<String> items = Arrays.asList(dataValues.split("\\s*,\\s*"));
            Iterator<String> it = items.iterator();
            while(it.hasNext()) {
                String value = it.next();
                String regex = "([0-9]|1[012])\\/([0-9]|[12][0-9]|[3][01])\\/(19|20)\\d\\d";
                Matcher matcher = Pattern.compile(regex).matcher(value);
                if (matcher.find()) {
                    CellStyle cellStyle = workbook.createCellStyle();
                    CreationHelper createHelper = workbook.getCreationHelper();
                    cellStyle.setDataFormat(
                        createHelper.createDataFormat().getFormat("m/d/yyyy"));
                    Cell cell = row.createCell(cellnum);
                    cell.setCellValue(value);
                    cell.setCellStyle(cellStyle);
                }
                else {
                    Cell cell = row.createCell(cellnum);
                    cell.setCellValue(value);
                }
                cellnum++;
            }
            try (FileOutputStream fileOut = new FileOutputStream(path + ".new")) {
                workbook.write(fileOut);
                fileOut.close();
            }
            workbook.close();
            FileUtilities fileUtil = new FileUtilities();
            fileUtil.deleteFile(path+ ".new");

        }catch (EncryptedDocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }