根據名稱中的字符串刪除多列你好,
我需要根據它們包含的字符串刪除多個列,但有時我想保留其他一些列,即使它們不包含這些字符串。
因此,按照這篇文章的解決方案根據列名刪除多列
dt = Open( "$sample_data/Hollywood Movies.jmp" );
col_names = dt << Get Column Names( String );
For( i = N Items( col_names ), i > 0, i--,
If(
Not(
Contains( col_names[i], "Gross") | contains(col_names[i], "Genre" ) | col_names[i] == "theme"),
Remove From( col_names, i)
)
);
所以,我想要的是刪除任何不包含“Gross”或“Genre”的內容,但如果列名是==“主題”,則不要刪除它。
看來暫時還不行啊! 為什麼?

這個問題的第二個要點
如何將 col_names 視為數據表,以便我無需將鼠標懸停在 col_names 上即可知道輸出是什麼。 因為有時我有一個很長的列名稱列表,並且懸停並不能有效地查看該列列表中的內容。
謝謝!
大規模定制回复:根據名稱中的字符串刪除多列你對數據表的困惑是我以前見過的。JMP 及其數據表的不同之處在於,需要將數據表視為基本元素。很多時候,人們會看到一行 JSL,例如:
dt = Open("$SAMPLE_DATA/big class.jmp");
在某些語言中,dt 是數據表。不是這種情況。上面的語句只是分配了一個指向數據表“Big Class”的指針。如果你將鼠標懸停在變量 dt 上你會看到
data table( "Big Class" )
因為它的價值。
因此,如果我運行該語句
xyz = dt;
我沒有像在某些語言中那樣獲得數據表 Big Class 的新實例,相反,我得到的只是名為 xyz 的變量,它是指向名為 Big Class 的同一數據表的指針。
要創建一個新副本(它不是一個新實例,因為一旦創建,它通常是一個完全獨立的新數據表),可以使用表平台之一來完成任務
dt2 = dt << subset( all rows, all columns);
上面的命令創建一個新的數據表,並將變量 dt2 設置為指向新表的指針。
回复:根據名稱中的字符串刪除多列吉姆,
我為我的誤會道歉。 但我搜索的大部分內容在 jsl pdf 中沒有得到很好的描述或只是簡單地解釋。 特別是當您想要執行複雜的操作時,遞歸腳本(if-else、for 循環)不會包含在手冊中。 跟踪錯誤也非常困難。 直到你說檢查“日誌”我才這麼做。
例如,沒有說明如何創建新數據表或如何分配新數據表。
我的想法是直接的
dt_new << Data Table( col_names );
應該創建一個新的數據表,但我了解到它在 jmp 中並不直接。 我們需要詢問或練習我正在做的語法。
多謝!
回复:根據名稱中的字符串刪除多列如果在創建後立即打印“col_names”的值,它將顯示數據表中的所有列
{"Movie Name", "Lead Studio Name", "Rotten Tomatoes Score", "Audience Score",
"Theme", "Genre", "Theaters Opening Wknd", "BOA Opening Wknd", "Domestic Gross",
"Foreign Gross", "World Gross", "Production Budget", "Profitability",
"Opening Wknd Gross"}
如果仔細觀察,您會發現沒有包含值“主題”的條目。 有一個值為“主題”的條目。您正在比較字符串中的值,並且必須正確匹配大寫和小寫。
我強烈建議您閱讀腳本指南!
幫助==>JMP 文檔庫腳本編寫指南
回复:根據名稱中的字符串刪除多列為什麼
col_names[i] == "theme"<BR />
是不是還在工作?

回复:根據名稱中的字符串刪除多列dt = Open( "$sample_data/Hollywood Movies.jmp" );
col_names = dt << Get Column Names( String );
dt:Profitability << delete formula;
For( i = N Items( col_names ), i > 0, i--,
If(
Not(
Contains( col_names[i], "Gross") | contains(col_names[i], "Genre" ) | col_names[i] == "theme"),
Remove From( col_names, i)
)
);
// At this point in the script, you have a data table
// with all of the original columns(Profitability is now a static column)
// And you have a JSL List called "col_names", which is stored in memory
// so you can do anythin you want given this
// You could just write the contents of the list to the log. Remember, the
// list "col_names", only contains the names of the columns, it does not
// contain the whole column(name, properties and data)
Print( col_names );
// or
Write( col_names );
// or
Show( col_names );
// That will give you the list of columns that are going to be deleted
// or before you want to see the columns in their entirety you can create
// a new data table with just those columns
newdt = dt << subset( all rows, columns( col_names ) );
// The statement below, is a perfectly valid statement, however, it will
// not work the way you think it will. The end result of this statement
// is to create a new list of column names.
// {"Genre", "Domestic Gross", "Foreign Gross", "World Gross", "Opening Wknd Gross"}
deleted_columns = delete columns( col_names );
回复:根據名稱中的字符串刪除多列嗨吉姆,
感謝您的回答。 好吧,我明白你說的了。
假設我們不關心公式。 讓我們刪除所有公式連接並刪除我想要的列。
我想創建(並查看)那些已刪除的列作為數據表
按照你的建議
dt = Open( "$sample_data/Hollywood Movies.jmp" );
col_names = dt << Get Column Names( String );
dt:Profitability << delete formula;
For( i = N Items( col_names ), i > 0, i--,
If(
Not(
Contains( col_names[i], "Gross") | contains(col_names[i], "Genre" ) | col_names[i] == "theme"),
Remove From( col_names, i)
)
);
deleted_columns = delete columns( col_names );
這仍然不能創建我需要的:(
回复:根據名稱中的字符串刪除多列從您提供的 JSL 中,您遺漏了最關鍵的語句
dt << delete columns( col_names );
這是實際執行刪除操作的行。但是,在此示例案例中,它還有一個額外的問題。最終的列列表
{"Genre", "Domestic Gross", "Foreign Gross", "World Gross", "Opening Wknd Gross"}
其中有“World Gross”列,用於計算“Profitability”列。處理“刪除列”消息時,它會檢測到問題並顯示在日誌中
Cannot delete the selected columns while some are referenced by the formulas of remaining columns.
Removing a formula leaves the data unchanged. Removing references replaces each reference with an empty value. These effects are permanent and cannot be undone.
如果您從“盈利能力”列中刪除公式,它會將值轉換為靜態值,
dt:Profitability << delete formula;
那麼“刪除列”就可以正常工作了。
關於將鼠標懸停在變量上以查看其值的問題,並希望創建一個數據表以便能夠查看這些值,有一種更簡單的方法可以做到這一點。您不需要將鼠標懸停在“col_names”變量上,而是突出顯示它,然後右鍵單擊並選擇“運行腳本”。按照規則,如果 JMP 腳本中的任何內容被突出顯示,“運行腳本”將僅運行突出顯示的行/語句/變量。結果將顯示在日誌中。