在矩阵列中找到模式你好!
我需要一些帮助编写脚本以找到几个矩阵中最常见的值。我有一个包含数百行的数据表。每行都有一个序列列(例如ABCDABCD),我创建了一个列来显示该序列中某个字母的位置(例如,A的位置是[1,5,0,0])。
(我在编写脚本时相当新,所以现在我的矩阵在没有更多位置包含该字母的情况下列出了0。如果有一种明显的,更有效的方法来查找字符串中的多个位置,请告诉我。否则,有没有办法将0排除为其中一种模式?)
我现在想看看所有行中调出的所有位置并找到最常见的位置。
是)我有的:
行矩阵列
1 [2,3,6,8,0,0]
2 [2,4,5,10,0,0]
3 [2,7,9,10,0,0]
我想要的是:
列表以大多数事件的顺序显示最少的模式
位置2 - 3次出现
位置10 - 2次出现
我附上了我目前拥有的包含位置查找公式的脚本,
谢谢!
Re:在矩阵列中找到模式谢谢!!Re:在矩阵列中找到模式dt << select where(:( letter ==“A”&:Row Num == 1)|(:Letter ==“C”&:Row Num == 2);
这不是第一个要求吗?
如果我正确理解你想要的东西,你的第二个要求应该可以使用Tabulate来计算
Re:在矩阵列中找到模式我需要能够逐行找到不同字母的位置,然后比较行。例如,我需要找到第1行序列中所有“A”的位置以及第2行序列中“C”的所有位置。然后我需要能够看到表中最常出现的位置。
从你给我的脚本(顺便说一句,谢谢!)我仍然无法做这个表广泛比较。
Re:在矩阵列中找到模式下面是一个脚本,它使用不同的方法将您的所有Letter值解析为一个新列,您可以从中进行分析
dt=current data table();
// Create a new table with just the Sequence column in it
dtbuild = dt << subset(columns("Sequence"), selected rows(0));
// Find the longest sequence
Maxstring = col max(length(:sequence));
// Add a new column called Row Num that will let you keep track of what row
// each letter came from
dtBuild << New column("Row Num", formula(row()));
// Convert the data into real values
dtBuild:Row Num << delete property("formula");
// Create a new column for each possible position in the sequence number, and
// strip off the ith character and place it into the column
For(i=1,i<=Maxstring,i++,
dtBuild << new column("C" || char(i),character, formula(substr(:sequence,eval(i),1)));
column(dtBuild,i) << delete property("formula");
);
// Create a list of the columns in the dtBuild data table
TheColumns = dtBuild << get column names();
// We are only going to stack the columns created that have bits of the
// sequence in them, so get rid of the names of the first 2 column in the data table
TheColumns = remove(TheColumns,1,2);
// Stack all of the columns in to a data table that can be worked with
dtFinal = dtBuild << Stack(
columns( TheColumns),
Source Label Column( "Position" ),
Stacked Data Column( "Letter" ),
Name( "Non-stacked columns" )(Keep( :Row Num ))
);
// Get rid of the interium table
close(dtBuild, nosave);
// Delete rows that have no letter value
dtFinal << select where(:Letter == "");
dtFinal << delete rows;