從列標題中提取字串並添加到另一個列標題中你好,
我有一個導入腳本,用於將結果從 Excel 匯入到 JMP 中。 Excel範本被很多人使用,並且被鎖定以進行編輯。我們在 Excel 中有兩個結果列,它們都稱為“結果 - XXX”,其中 XXX 使用 Excel 巨集替換為正在研究的內容,因此每個文件中都會發生變化。我們也有名為 JMP 結果 1 和 JMP 結果 2 的列,它們與結果列相同並且採用數字格式。我們使用的 JMP 匯入無法從 Excel 匯入科學數字。當我在 JMP 中開啟 Excel 時,我得到如下圖所示的內容:

我正在嘗試編寫一個腳本,該腳本將從列標題中提取“結果 -”後面的單字並替換 JMP 結果列標題,但確保它替換正確的標題。所以結果是:

不確定這在 JMP 中是否可行,但我們將不勝感激。甚至不知道如何從提取列標題字串的末尾開始,更不用說後面的步驟了。提前致謝。
自動化和腳本編寫回覆:從列標題中提取字串並添加到另一個列標題中這是純 JSL 選項。它可以(並且將會)很容易破壞,這取決於您的數據,因為我必須做出很多假設
Names Default To Here(1);
dt = New Table("Untitled",
Add Rows(1),
Compress File When Saved(1),
New Column("Results - DNA", Numeric, "Continuous", Format("Best", 12), Set Values([.]), ),
New Column("Results - Protein", Numeric, "Continuous", Format("Best", 12), Set Values([.])),
New Column("JMP Result 1", Numeric, "Continuous", Format("Best", 12), Set Values([.])),
New Column("JMP Result 2", Numeric, "Continuous", Format("Best", 12), Set Values([.]))
);
// Assume that you extract last word from result columns
// and results are first then jmp columns and there is equal amount
result_columns = {};
jmp_columns = {};
colnames = dt << Get Column Names("String");
For Each({colname}, colnames,
If(Starts With(colname, "Results - "),
Insert into(result_columns, Word(-1, colname, " "));
,
Insert Into(jmp_columns, colname)
)
);
For Each({{rescol, jmpcol}}, Across(result_columns, jmp_columns),
Column(dt, jmpcol) << Set Name(rescol);
);
Write();回覆:從列標題中提取字串並添加到另一個列標題中你好,
我附上測試數據表。試試這個腳本:
dt = current data table();
for each({name,i}, dt << get column names(string),
if (contains(name, "Results -"),
new_name_string = word(2, name, "- ");
column(dt,i) << set name(substitute(name, "Results -", "Results_Excel -" ));
for each({name2,p}, dt << get column names(string),
if (contains(name2, "JMP Result"),
column(dt,p) << set name(new_name_string);
Break();
,
);
);
,
);
);
這應該可以達到您的目標。如果沒有,請告訴我們。
編輯:剛剛看到 Jarmos 很棒的解決方案。如果您不想變更「excel」列的名稱,則可以選擇此選項。
乾杯,
喬納斯
回覆:從列標題中提取字串並添加到另一個列標題中我還想到了非腳本選項。
建立堆疊表

從標籤建立最後一個單字列

現在你有了映射表

返回原始表並重新編碼列名稱

使用對錶應用映射...


然後點擊重新編碼


不幸的是,JMP 無法自動為此創建正確的腳本,因為它只會直接在腳本中使用映射值,因此這可能是手動過程,除非您能夠以某種方式創建「完整映射表」以將其填充到劇本
// Recode column names
Local({dt = Data Table("Untitled 2"), names},
names = Recode(
dt << Get Column Names(String),
{Map Value(
_rcOrig,
{"JMP Result 1", "DNA", "JMP Result 2", "Protein"},
Unmatched(_rcNow)
)}
);
For Each({name, i}, names, Column(dt, i) << Set Name(name));
);