获取列中的所有匹配值

时间:2017-03-29 14:40:19

标签: r match

我有一个名为 toy<- structure(list(id = 1:10, Name = c("A", "B", "C", "D", "E", "F", "G", "H", "A", "A"), Alt = c("X|Y|a", "O|P|dev", "A|W|are", "M|Q|G", "H|f|j|i_m|am", "L|E|B|i|j", "x|C|xx|yy", NA, NA, NA), Place = c(1L, 4L, 8L, 12L, 13L, 8L, 3L, 1L, 1L, 1L)), .Names = c("id", "Name", "Alt", "Place"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L), spec = structure(list(cols = structure(list(id = structure(list(), class = c("collector_integer", "collector")), Name = structure(list(), class = c("collector_character", "collector")), Alt = structure(list(), class = c("collector_character", "collector")), Place = structure(list(), class = c("collector_integer", "collector"))), .Names = c("id", "Name", "Alt", "Place")), default = structure(list(), class = c("collector_guess", "collector"))), .Names = c("cols", "default"), class = "col_spec")) 的数据框,如下所示:

Name

我的目的是在Alt列中查找同样位于dplyr列中的匹配字符。我使用toy_sep<-toy %>% separate(Alt , into=LETTERS[1:5],sep="\\|",extra="merge",remove=FALSE) %>% gather(Alias_id,Alias,A:E) %>% mutate(Match=match(Alias,Name)) 尝试了以下内容:

matches<-toy_sep[complete.cases(toy_sep),]

从这里看,有一个匹配的输出如下:

match

它接近我想要的东西。但问题是matches返回第一个位置,而我想要所有匹配。在示例1中,在id数据框的匹配列中为A返回,但我想要所有ID。 A有id 9和10(来自toy数据框中的d_out<-structure(list(id = c(3L, 5L, 6L, 7L, 4L, 6L), Name = c("C", "E", "F", "G", "D", "F"), Alt = c("A|W|are", "H|f|j|i_m|am", "L|E|B|i|j", "x|C|xx|yy", "M|Q|G", "L|E|B|i|j"), Place = c(8L, 13L, 8L, 3L, 12L, 8L), Alias_id = c("A", "A", "B", "B", "C", "C"), Alias = c("A", "H", "E", "C", "G", "B"), Match = c("1|9|10", "8", "5", "3", "7", "2")), class = c("tbl_df", "tbl", "data.frame" ), row.names = c(NA, -6L), .Names = c("id", "Name", "Alt", "Place", "Alias_id", "Alias", "Match"), spec = structure(list(cols = structure(list( id = structure(list(), class = c("collector_integer", "collector" )), Name = structure(list(), class = c("collector_character", "collector")), Alt = structure(list(), class = c("collector_character", "collector")), Place = structure(list(), class = c("collector_integer", "collector")), Alias_id = structure(list(), class = c("collector_character", "collector")), Alias = structure(list(), class = c("collector_character", "collector")), Match = structure(list(), class = c("collector_character", "collector"))), .Names = c("id", "Name", "Alt", "Place", "Alias_id", "Alias", "Match")), default = structure(list(), class = c("collector_guess", "collector"))), .Names = c("cols", "default"), class = "col_spec")) 列)以及1.感谢您使用base / data.table / dplyr的任何帮助

添加所需的输出。请注意,右上角单元格中的数字不需要用&#34; |&#34; 。 :

LatentDirichletAllocation

1 个答案:

答案 0 :(得分:1)

试试这个。

// solution for array of objects
var elems = [{car1: 2}, {car2: 3}, {truck: 1}, {bike: 2}];
var filtered = elems.reduce(function (result, current) {
  var isCar = Object.keys(current).some(function (key){
    return key.indexOf("car") > -1;
  });
  if (isCar) {
    result.push(current);
  }
  return result;
}, []);
// returns an array of "car" objects
console.log(filtered)

// solution for single object with multiple properties
var elems2 = {car1: 2, car2: 3, truck: 1, bike: 2};
var filtered2 = Object.keys(elems2).reduce(function (result, current) {
  var isCar = current.indexOf("car") > -1;
  if (isCar) {
    result[current] = elems2[current];
  }
  return result;
}, {});
// returns single object with only "car" properties and their values
console.log(filtered2)