0
所有匹配值我有一個名爲toy
像這樣一個數據幀:充分利用列
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
返回第一個位置,而我想要所有的匹配。在示例1中,在A的matches
數據框的匹配列中返回,但我想要所有的ID。 A的id爲9和10(來自toy
dataframe中的id
列)以及1.感謝您使用base/data.table/dplyr的任何幫助
添加所需的輸出。請注意,右上角單元格中的數字不需要用「|」分隔。 :
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"))
請問您可否在本示例數據中包含您想要的輸出? – lmo