根据名称中的字符串删除多列你好,
我需要根据它们包含的字符串删除多个列,但有时我想保留其他一些列,即使它们不包含这些字符串。
因此,按照这篇文章的解决方案根据列名删除多列
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 脚本中的任何内容被突出显示,“运行脚本”将仅运行突出显示的行/语句/变量。结果将显示在日志中。