第一个和第二个“ _”之间的正则表达式

时间:2018-10-10 20:01:12

标签: r regex

大家好,我有以下数据:

data                   condition variable
proprio_com_luz           com        luz
proprio_sem_ola_acabo     sem        ola_acabo

我想创建两个新变量

var source = {
  localdata: [
    ["Test1", "2018-08-29 14:19:07", "2020-08-29 14:19:07", "Path1"],
    ["Test2", "2018-09-05 11:26:39", "2020-09-05 11:26:39", "Path2"],
    ["Test3", "2018-08-30 07:32:23", "2020-08-30 07:32:23", "Path3"],
    ["Test4", "2018-09-11 09:01:42", "2020-09-11 09:01:42", "Path4"],
    ["Test5", "2018-08-01 15:28:22", "2020-08-01 15:28:22", "Path5"],
    ["Test6", "2018-08-01 15:28:22", "2020-08-01 15:28:22", "Path6"],
    ["Test7", "2018-09-13 07:34:57", "2020-09-13 07:34:57", "Path7"]
  ],
  datafields: [{
      name: 'dataSetName',
      type: 'string',
      map: '0'
    },
    {
      name: 'accessStartDate',
      type: 'date',
      map: '1'
    },
    {
      name: 'accessEndDate',
      type: 'date',
      map: '2'
    },
    {
      name: 'conceptPath',
      type: 'string',
      map: '3'
    }
  ],
  datatype: "array"
};
var dataAdapter = new $.jqx.dataAdapter(source, {
  loadComplete: function(data) {},
  loadError: function(xhr, status, error) {}
});

$("#main_downloader_grid").jqxGrid({

  source: dataAdapter,
  width: 381,
  height: '200',
  pageable: true,
  sortable: true,
  autoheight: true,
  columnsResize: true,
  theme: 'classic',
  columns: [{
      text: 'Data Set',
      datafield: 'dataSetName',
      width: 140

    },
    {
      text: 'Start Date',
      datafield: 'accessStartDate',
      width: 120,
      cellsformat:'MM/dd/yyyy'
    },
    {
      text: 'End Date',
      datafield: 'accessEndDate',
      width: 120,
      cellsformat:'MM/dd/yyyy'
    },
    {
      text: 'Concept Path',
      datafield: 'conceptPath',
      width: 50,
      hidden: true
    }
  ]

});

// Row Select Logic Starts Here
$("#main_downloader_grid").on("rowselect", function(e) {
  let data_set_name = $("#main_downloader_grid").jqxGrid('getcell', e.args.rowindex, 'dataSetName');

  console.log("Cell Value Test");
  console.log(data_set_name.value);


  let conceptPath = $("#main_downloader_grid").jqxGrid('getcell', e.args.rowindex, 'conceptPath');
  console.log("Concept Path Test");
  console.log(conceptPath.value);


  $('#commonWindow').remove();
  //$('.clickable').remove();
  $('.clickable').next('#testbutton').remove();
    $('.clickable').slice(1).remove();


  var elem = document.createElement('div');
  elem.id = 'commonWindow';
  //elem.setAttribute('style', 'margin:500px 10px 20px 20px;');


  console.log(elem);



  let data = [{
      letter: '<b>People</b>'
    },
    {
      letter: '1) Detailed demographics data of all people'
    },
    {
      letter: '2) Attributes associated with all people'
    },
    {
      letter: '<b>Technology</b>'
    },
    {
      letter: '1) Computer details'
    },
    {
      letter: '2) Hardware Details'
    },
    {
      letter: '3) Software Details'
    },
    {
      letter: '<b>Company Details</b>'
    }
    /* {conceptpath: conceptPath.value } */
  ];
  let source = {
    localdata: data,
    datatype: "array",
    datafields: [{
      name: 'letter',
      type: 'string'
    } /* ,{ name: 'conceptpath', type: 'string' } */ ]
  };
  let newDataAdapter = new $.jqx.dataAdapter(source);

  $(elem).jqxGrid({
    source: newDataAdapter,
    width: 395,
    height:310,
    columns: [{
        text: 'Data set <b>' + data_set_name.value + '</b> selected, what data do you want to see?',
        datafield: 'letter'
        //width: 450
      }

    ]
  });
 
 document.body.appendChild(elem);
  // elem.id.style = "margin:50px 10px 20px 30px";
  elem.style.margin = "50px 10px 20px 50px";

$("#commonWindow").on("rowselect", handleClick);

function handleClick(e) {
  var $el = $("<div />", {
    class: "clickable",
    style: "margin:100px 10px 20px 20px ",
  })
  .on('click', handleClick)

  $el.jqxGrid({
    height: 270,width:520, pageable: true,source: dataAdapter, columns: [
      { text: 'Data Set Name', datafield: 'dataSetName', width: 200 },
      { text: 'Access Start Date', datafield: 'accessStartDate', width: 150,cellsformat:'MM/dd/yyyy' },
      { text: 'Access End Date', datafield: 'accessEndDate', width: 150,cellsformat:'MM/dd/yyyy' },
      { text: 'Concept Path', datafield: 'conceptPath', width: 100,hidden:true }
     
    ]
  });

  var $this = $(this), $parent = $(this).parent();

  if (e.type == 'rowselect') {
     $('.clickable').next('#testbutton').remove();
    $('.clickable').slice(1).remove();
  }

  var $button = $("<div id = 'testbutton'></div>").on('click', function (e) {
       $(".clickable").jqxGrid('exportdata', 'csv', 'jqxGrid');
  });
  
 
  console.log($button);

  
  $button.jqxButton({ width: 100, height: 20});
    $button.html('Download Data');

  $el.after($button);

    $parent.append($el);
  $(this).off('click');
}


});

什么正则表达式可以帮助我?

2 个答案:

答案 0 :(得分:4)

使用extract中的tidyr

library(tidyr)

extract(df, data, c("condition", "variable"),
        regex = "^[^_]+_([^_]+)_(.+)$", remove = FALSE)

或以R为底的

pattern <- "^[^_]+_([^_]+)_(.+)$"

df$condition = sub(pattern, "\\1", df$data)
df$variable = sub(pattern, "\\2", df$data)

输出:

                   data condition  variable
1       proprio_com_luz       com       luz
2 proprio_sem_ola_acabo       sem ola_acabo

数据:

df <- data.frame(data = c("proprio_com_luz",
                          "proprio_sem_ola_acabo"))

答案 1 :(得分:1)

如果您不熟悉正则表达式,则可以使用这种(不是简短的)方法

> string <- c("proprio_com_luz", "proprio_sem_ola_acabo")
> out <- do.call(rbind, lapply(strsplit(string, "_"), function(x) c(x[2], paste0(x[-c(1,2)], collapse="_"))))
> data.frame(data=string, condition=out[, 1], variable=out[, 2])
                   data condition  variable
1       proprio_com_luz       com       luz
2 proprio_sem_ola_acabo       sem ola_acabo