我查看了Maurits的答案,但是当我添加新行时。
新输入
D%GT 12434 12421 22 DXGT
DX%T 31242 2221.2 21 DXGT
使用的数据
pattern Freq score rank true_string
DT%E 37568 1138.4242 1 DTGE
D%GT 12434 12421 22 DXGT
DX%T 31242 2221.2 21 DXGT
%TGE 37666 1018 2 DTGE
D%GE 37641 1017.3243 3 DTGE
DTG% 37665 965.7692 4 DTGE
%VGNE 34234 684.68 5 SVGNE
SVGN% 34281 634.8333 6 SVGNE
SV%NE 34248 634.2222 7 SVGNE
SVG%E 34265 623 8 SVGNE
%LGNE 41098 595.6232 9 SLGNE
SL%NE 41086 595.4493 10 SLGNE
SLGN% 41200 564.3836 11 SLGNE
SPT%AYNE 35082 539.7231 12 SPTAAYNE
SP%AAYNE 35094 531.7273 13 SPTAAYNE
SPTA%YNE 35061 531.2273 14 SPTAAYNE
SPTAA%NE 35225 518.0147 15 SPTAAYNE
SPTAAYN% 35144 516.8235 16 SPTAAYNE
%PTAAYNE 35111 516.3382 17 SPTAAYNE
S%TAAYNE 35100 516.1765 18 SPTAAYNE
SPTAAY%E 35130 509.1304 19 SPTAAYNE
SLG%E 41467 450.7283 20 SLGNE
莫里茨的回答
df %>%
mutate(grp = cutree(hclust(stringdistmatrix(df$pattern, method = "qgram")), h = 2)) %>%
group_by(grp) %>%
mutate(true_string = get_consensus_string(pattern)) %>%
ungroup()
> Result
pattern Freq score rank grp true_string
1 DT%E 37568 1138. 1 1 DT%T
2 D%GT 12434 12421 22 1 DT%T
3 DX%T 31242 2221. 21 1 DT%T
4 %TGE 37666 1018 2 2 %TGE
5 D%GE 37641 1017. 3 2 %TGE
6 DTG% 37665 966. 4 1 DT%T
7 %VGNE 34234 685. 5 3 SVGNE
8 SVGN% 34281 635. 6 3 SVGNE
9 SV%NE 34248 634. 7 3 SVGNE
10 SVG%E 34265 623 8 3 SVGNE
11 %LGNE 41098 596. 9 4 SLGNE
12 SL%NE 41086 595. 10 4 SLGNE
13 SLGN% 41200 564. 11 4 SLGNE
14 SPT%AYNE 35082 540. 12 5 SPTAAYNE
15 SP%AAYNE 35094 532. 13 5 SPTAAYNE
16 SPTA%YNE 35061 531. 14 5 SPTAAYNE
17 SPTAA%NE 35225 518. 15 5 SPTAAYNE
18 SPTAAYN% 35144 517. 16 5 SPTAAYNE
19 %PTAAYNE 35111 516. 17 5 SPTAAYNE
20 S%TAAYNE 35100 516. 18 5 SPTAAYNE
21 SPTAAY%E 35130 509. 19 5 SPTAAYNE
22 SLG%E 41467 451. 20 4 SLGNE
从上面的结果来看,它不起作用。
我的答案
library(dplyr)
library(data.table)
df <- fread(data)
string_pred <- function(x){
x = x %>% mutate(CL=nchar(pattern))
x_1 = x%>% select(pattern,CL)
Chr.length = unique(x_1$CL)
final_result = NULL
for ( len in 1:length(Chr.length)){
x_1_tmp = x %>% filter(CL==Chr.length[len])
RESULT = NULL
for(i in 1:Chr.length[len]){
TMP = substr(x_1_tmp$pattern,i,i)
TMP_GUESS = unique(TMP[!grepl("%",TMP)])
if(length(TMP_GUESS)==1){
TMP[grepl("%",TMP)] <- TMP_GUESS
} else {
TMP= TMP
}
NAME = sprintf('P%s',i)
RESULT = cbind(RESULT, NAME=TMP) %>% as.data.table()
names(RESULT)[i] = eval(parse(text='NAME'))
}
material = RESULT %>% rowwise() %>% .[apply(.,1,function(x){'%' %in% x}) ,]
if (nrow(material)==0){
x_1_tmp =x_1_tmp %>% mutate( pred = apply(RESULT,1,function(x)paste(as.character(x),collapse = ''))) %>% as.data.table()
} else {
mat.loc = RESULT %>% rowwise() %>%apply(.,1,function(x){'%' %in% x}) %>% which(unlist(.)==TRUE)
for (i in 1:nrow(material)){
ori.loc = mat.loc[i]
loc = names(material[i,])[material[i,]=='%']
tmp = material[i,] %>% dplyr::select(-loc)
RESULT[ori.loc,] = RESULT %>% rowwise() %>% inner_join(., tmp) %>% .[apply(.,1,function(x){!('%' %in% x)}) ,] %>% unique()
}
x_1_tmp = x_1_tmp %>%mutate( pred = apply(RESULT,1,function(x)paste(as.character(x),collapse = ''))) %>% as.data.table()
}
final_result = rbind(final_result, x_1_tmp)
}
return(final_result)
}
我的回答结果
> string_pred(df)
pattern Freq score rank CL pred
1: DT%E 37568 1138.4242 1 4 DTGE
2: D%GT 12434 12421.0000 22 4 DXGT
3: DX%T 31242 2221.2000 21 4 DXGT
4: %TGE 37666 1018.0000 2 4 DTGE
5: D%GE 37641 1017.3243 3 4 DTGE
6: DTG% 37665 965.7692 4 4 DTGE
7: %VGNE 34234 684.6800 5 5 SVGNE
8: SVGN% 34281 634.8333 6 5 SVGNE
9: SV%NE 34248 634.2222 7 5 SVGNE
10: SVG%E 34265 623.0000 8 5 SVGNE
11: %LGNE 41098 595.6232 9 5 SLGNE
12: SL%NE 41086 595.4493 10 5 SLGNE
13: SLGN% 41200 564.3836 11 5 SLGNE
14: SLG%E 41467 450.7283 20 5 SLGNE
15: SPT%AYNE 35082 539.7231 12 8 SPTAAYNE
16: SP%AAYNE 35094 531.7273 13 8 SPTAAYNE
17: SPTA%YNE 35061 531.2273 14 8 SPTAAYNE
18: SPTAA%NE 35225 518.0147 15 8 SPTAAYNE
19: SPTAAYN% 35144 516.8235 16 8 SPTAAYNE
20: %PTAAYNE 35111 516.3382 17 8 SPTAAYNE
21: S%TAAYNE 35100 516.1765 18 8 SPTAAYNE
22: SPTAAY%E 35130 509.1304 19 8 SPTAAYNE
方法
- 按照每个模式的字符长度进行分离
pattern Freq score rank CL
1 DT%E 37568 1138.4242 1 4
2 D%GT 12434 12421.0000 22 4
3 DX%T 31242 2221.2000 21 4
4 %TGE 37666 1018.0000 2 4
5 D%GE 37641 1017.3243 3 4
6 DTG% 37665 965.7692 4 4
- 逐个检查每个字符。
TMP = substr(x_1_tmp$pattern,i,i)
[1] "D" "D" "D" "%" "D" "D"
如果 unique(pattern[i] except % ) == 1
--> 那么我们将分配%给unique(pattern[i] except % )
P1 P2 P3 P4
1: D T G E
2: D % G T
3: D X G T
4: D T G E
5: D % G E
6: D T G %
unique(pattern[i] except % ) > 1
我们检查字符长度组中的其他行。然后我们将字符(除了%列)合并到其他字符中。
RESULT[ori.loc,] = RESULT %>% rowwise() %>%
inner_join(., tmp) %>%
.[apply(.,1,function(x){!('%' %in% x)}) ,] %>% unique()
>print
Joining, by = c("P1", "P3", "P4")
Source: local data frame [1 x 4]
Groups: <by row>
P1 P2 P3 P4
<chr> <chr> <chr> <chr>
1 D X G T
- 最后我们可以预测出什么是
%
pattern Freq score rank CL pred
1: DT%E 37568 1138.4242 1 4 DTGE
2: D%GT 12434 12421.0000 22 4 DXGT
3: DX%T 31242 2221.2000 21 4 DXGT
4: %TGE 37666 1018.0000 2 4 DTGE
5: D%GE 37641 1017.3243 3 4 DTGE
6: DTG% 37665 965.7692 4 4 DTGE
我的回答看起来不太花哨,但是它是有效的。
我建议你一步一步地跟着代码走。
SVGNE
和SLGNE
似乎特别棘手。 - thelatemailS%GNE
。 - Calum You