列標題搜索和替換你好,
我可以使用確切的名稱替換列標題名稱:高度<<設置名稱(“測試”);
如果我有一個文本列表,我想將其替換為“”。
我該如何處理?
“姓名”“時間”“性別”“身高abc體重”“體重xyz”“12身高測試”
凱蒂 12 F 59 95 59
路易絲 12 F 61 123 61
最終輸出:
“姓名”“時間”“性別”“abc”“xyz”“12測試”
Names Default To Here( 1 );
dt = Open( "C:/JSL/BigClass.jmp" );
headerList = dt << get column names( string );
//:height << Set Name( "test" );
For( i = 1, i <= N Items( headerList ), i++,
If( (headerList[i] == "height" || headerList[i] == "weight"), //trying to find any column header has height or weight and replace with ""
Column( dt, colList[i] ) << set name( " " ) //above are just two examples, maybe put it in list and loop?
)
);
腳本回复:列標題搜索和替換你好山姆,
問題似乎出在您仍在進行替換的第二個循環 (j) 中直接重命名列。
一種可能的解決方案(如下所示)是修改標題列表 (hl),然後在完成所有更改後重命名該列:
Names Default To Here( 1 );
dt = Open( "C:\Program Files\SAS\JMP\14\Samples\Data\Big Class.jmp" );
hl = dt << get column names( string );
sr = List();
sr[1] = "height"; //
sr[2] = "e"; // Order of replacement strings is critical
sr[3] = "a"; //
For( i = 1, i <= N Items( hl ), i++,
For (j = 1, j <= N Items (sr), j++,
If (contains (hl [i], sr [j]),
txt = substitute (hl [i], sr [j], "x"); // Just for clarity: the expression could be wrapped into the next
Substitute into (hl, hl [i], txt); // Hold all the changes into the hl list
)
);
Column (dt , i ) << set name (hl [i]); // Renames column only after all changes have been made
);
我希望它有所幫助。
最好,
TS
回复:列標題搜索和替換我試圖提前定義列表並進行雙嵌套循環。 它只能替代字符串替換 [3]。
headerList = dt << get column names( string );
stringReplace = List();
stringReplace[1] = "height";
stringReplace[2] = "e";
stringReplace[3] = "a";
For( i = 1, i <= N Items( headerList ), i++,
For( j = 1, j <= N Items( stringReplace ), j++,
Column( dt, i ) << set name( Substitute(headerList[i], stringReplace[j], "" ) )
);
);
回复:列標題搜索和替換你好山姆,
我在你的腳本中發現了 2 個問題:
- 您使用了連接“||”代表您的“或”語句的代碼(應為“|”)
- JMP 不允許將列命名為“”。
此外,通常更快更容易地按編號調用列而不是遍歷列名列表
所以,這就是我想出的:
Names Default To Here( 1 );
dt = Open( "C:\Program Files\SAS\JMP\14\Samples\Data\Big Class.jmp" );
headerList = dt << get column names( string );
For( i = 1, i <= N Items( headerList ), i++,
If( (headerList[i] == "height" | headerList[i] == "weight"), //Fixed the "Or" statement
Column( dt, i ) << set name(substitute (headerList [i], "e", "x")) //Used the substitute function as an example, you can replace any string with ""?
)
);