在一列矩陣中找到眾數你好!
我需要一些幫助來編寫腳本來查找多個矩陣中最常見的值。 我有一個包含數百行的數據表。 每行都有一個序列列(例如 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 次
我附上了我目前擁有的包含定位公式的腳本,
謝謝你!
腳本回复:在一列矩陣中查找眾數謝謝你!!回复:在一列矩陣中查找眾數dt << 選擇哪裡(:(字母==“A”&:行號==1)|(:字母==“C”&:行號==2);
這不是給了你第一個要求嗎?
如果我正確理解你想要什麼,你的第二個要求應該能夠使用 Tabulate 來計算
回复:在一列矩陣中查找眾數我需要能夠按行定位不同字母的位置,然後比較行。 例如,我需要找到第 1 行序列中所有“A”的位置以及第 2 行序列中“C”的所有位置。 然後我需要能夠看到表格中最常出現的位置。
從你給我的腳本(順便謝謝你!)我仍然無法進行這個表範圍的比較。
回复:在一列矩陣中查找眾數這是一個腳本,它使用不同的方法讓您將所有字母值解析到一個新列中,您可以從中進行分析
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;