如何在 JSL 中编写查找和替换代码,以从列中删除文本并转换为数字格式。JMP 社区您好,
我每周导入一次带有电池电压列(名为“V1”)的 .txt 数据,其中行值可以是“3.45V”、“3.34V”、“3.12V”、“3.23V”等。 我手动使用“查找和替换”从数据中删除字母“V”,然后手动将数据类型从字符更改为数字。 我想我不太明白如何在我的脚本中包含 Replace() 或 mummer() ,因为我已经尝试为这个简单的任务创建一个有效的 JSL,但失败了。
回复:如何在 JSL 中编写查找和替换代码,以从列中删除文本并转换为数字格式。我确实在我的代码中发现了逻辑错误。下面是一个示例数据表,其中包含一个可就地转换值的小脚本。
Names Default To Here( 1 );
dt = New Table( "Example",
add rows( 100 ),
New Column( "col1", character, formula( Char( Random Integer( 78, 100 ) ) || "V" ) ),
New Column( "col2", character, formula( Char( Random Integer( 78, 100 ) ) || "V" ) )
);
dt<<RUN formulas=""></RUN>
回复:如何在 JSL 中编写查找和替换代码,以从列中删除文本并转换为数字格式。嗨,吉姆,你的代码不太有效,我不明白为什么。 我已经上传了 JMP 调试器的屏幕截图以及错误消息,如果您想继续解决此问题,还可以上传我的数据子集。 我对这个替代解决方案非常感兴趣!

回复:如何在 JSL 中编写查找和替换代码,以从列中删除文本并转换为数字格式。谢谢,丹,
我能够将其融入我的工作流程中:
Names Default To Here( 1 );
:Name( "V1" ) << Set Name( "orig V1" );
New Column( "V1",
Numeric,
"Continuous",
Format( "Fixed Dec", 6, 2 ),
Formula( Num( Word( 1, :orig V1, "V" ) ) )
);
我很感谢您的快速回复!
回复:如何在 JSL 中编写查找和替换代码,以从列中删除文本并转换为数字格式。看看这样的东西是否有效......它还没有经过充分测试,但应该接近工作
Names Default To Here( 1 );
dt = Current Data Table();
colNames = dt << get column names( string, character );
For( theCol = 1, thecol <= N Items( colNames ), theCol++,
For( theRow = 1, theRow <= N Rows( dt ), theRow++,
If( Right( Column( dt, theCol )[theRow], 1 ) == "V",
Column( dt, theCol )[theRow] = Substr(
Column( dt, theCol )[theRow],
1,
Length( Column( dt, theCol )[theRow] ) - 1
)
)
)
);
If( Is Missing( Num( Column( dt, theCol )[1] ) ) == 0,
Column( dt, theCol ) << data type( numeric ) << modeling type( continuous )
);回复:如何在 JSL 中编写查找和替换代码,以从列中删除文本并转换为数字格式。我一直热衷于首先以交互方式生成解决方案,然后编写脚本。 我知道这并不总是可能的,但你可以接近。
打开文本文件会给出在带有 JMP 表的源脚本中找到的脚本(当然是针对我的示例):
Open(
"C:\Testing.txt",
columns( New Column( "V1", Character, "Nominal" ) ),
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Tab, Comma, CSV( 1 ) ),
Strip Quotes( 1 ),
Use Apostrophe as Quotation Mark( 0 ),
Use Regional Settings( 0 ),
Scan Whole File( 1 ),
Treat empty columns as numeric( 0 ),
CompressNumericColumns( 0 ),
CompressCharacterColumns( 0 ),
CompressAllowListCheck( 0 ),
Labels( 1 ),
Column Names Start( 1 ),
Data Starts( 2 ),
Lines To Read( "All" ),
Year Rule( "20xx" )
)
)
您可以根据您的具体情况进行修改。
现在,为了删除 V 并转换为数字,我将为此创建第二列。 这样可以在电压格式因某种原因发生变化时进行错误检查。 此外,它还提供更好的数据可追溯性。 我使用以下公式创建了一个列:Num( Word( 1, :V1, "V" ) )。 然后,我查看了表面板中找到的“复制表脚本”,并提取了脚本的相关部分来创建新列。
在 JSL 中,这将是:
New Column( "Numeric V1",
Numeric,
"Continuous",
Format( "Best", 12 ),
Formula( Num( Word( 1, :V1, "V" ) ) )
)
将两者放在一起应该是您的脚本。