如何修復缺少冒號的持續時間:大家好,
我遇到了一個以前從未遇到過的問題…
我有一個 Excel 文件,其中包含 87 列持續時間數據,格式為 hh:mm。
然而,它們處於 hhmm 或 hmm <-- 注意缺少冒號:
- 如果時長小於 10:00,則以三位數表示(例如,9:59 輸入為 959)。
- 如果長度超過 9 分 59 秒,則為四位數(例如,11 分 52 秒輸入為 1152)。
如果是 1 列或 2 列,我可以應付,但 87 列就應付不了了。
請查看附件。
非常感謝您的幫忙!
克里斯蒂安,你好。
自動化和腳本編寫資料融合與清理回覆:如何修復缺少冒號的持續時間:有很多選擇(我覺得這些應該都可行)。
1. 將它們全部轉換為字串
2. 在起始位置加 0
3. 取最後 4 個字符
4. 插入“:”作為第三個字符
5. 將它們轉換為數值,並設定持續時間格式
Names Default To Here(1);
dt = Current Data Table();
cols = dt << Get Column Names("String");
For Each({colname}, cols,
Column(dt, colname) << Set Data Type("Character");
Eval(EvalExpr(
dt << Apply Formula(
Columns(Expr(NameExpr(AsColumn(dt, colname)))),
Formula(
s = Right("0" || Expr(NameExpr(AsColumn(dt, colname))), 4);
Left(s, 2) ||":"||Right(s,2)
),
Output("In Place")
) << Run Formulas;
));
);
For Each({colname}, cols,
Column(dt, colname) << Set Data Type(Numeric, Format("hr:m", 30), Input Format("hr:m")) << Set Modeling Type("Continuous");
);
或者
Names Default To Here(1);
dt = Current Data Table();
cols = dt << Get Column Names("String");
dt << Begin Data Update;
For Each({colname}, cols,
For Each Row(dt,
s = Char(Column(dt, colname)[]);
If(Column(dt, colname)[] < 1000,
s = "0" || s
);
Column(dt, colname)[] = In Format(Left(s, 2) || ":" || Right(s, 2), "hr:m");
);
Column(dt, colname) << Format("hr:m", 30);
);
dt << End Data Update;
或直接使用這些數字,然後將它們「轉換」成 JMP 時間格式。
Names Default To Here(1);
dt = Current Data Table();
cols = dt << Get Column Names("String");
dt << Begin Data Update;
For Each({colname}, cols,
For Each Row(dt,
val = Column(dt, colname)[];
h = Floor(val / 100);
m = Mod(val, 100);
t = h * In Hours(1) + m * In Minutes(1);
Column(dt, colname)[] = t;
);
Column(dt, colname) << Format("hr:m", 30);
);
dt << End Data Update;
回覆:如何修復缺少冒號的持續時間:最後一種方法也可以透過 JMP 以互動方式輕鬆完成。
1. 選擇所有列
2. 右鍵單擊列標題
3. 建立公式快速列
4. 使用相對樣式替換公式
h = Floor(_relative_from_Col 1 / 100);
m = Mod(_relative_from_Col 1, 100);
t = h * In Hours(1) + m * In Minutes(1);
5. 將輸出更改為就地數據

6. 點選“替換 x”
7. 使用“標準化屬性”將持續時間格式變更為“小時:分鐘”,因為目前使用“最佳格式”時僅顯示秒數。

8. 點選「確定」或「應用」後

JMP 還很貼心地為你寫腳本。
// Transform column
Data Table("example for JMP com") << Apply Formula(
Columns(Group(:Col 1, 2), Ref("_relative_from_Col 1")),
Formula(
h = Floor(_relative_from_Col 1 / 100);
m = Mod(_relative_from_Col 1, 100);
t = h * In Hours(1) + m * In Minutes(1);
),
Output("In Place")
) << Run Formulas;
// Standardize column attributes
Local({old dt = Current Data Table()},
Current Data Table(Data Table("example for JMP com"));
For Each({col, index}, {:Col 1, :Col 2}, col << Format("hr:m", 10));
Current Data Table(old dt);
);
回覆:如何修復缺少冒號的持續時間:我的做法是這樣的:
Names Default To Here( 1 );
dt = Current Data Table();
colNames = dt << get column names( contiuous, string );
For Each( {col}, colNames,
Show( col );
For Each Row(
As Column( col ) = Informat(
Substr( Char( As Column( Col ) ), 1, Length( Char( As Column( Col ) ) ) - 2 ) || ":" ||
Substr( Char( As Column( Col ) ), Length( Char( As Column( Col ) ) ) - 1, 2 ),
"hr:m"
)
);
column(dt,col) << format( "hr:m" );
);
回覆:如何修復缺少冒號的持續時間:嗨,Jarmo,非常感謝!
你的第一個劇本就產生奇效了!
但我剛剛發現,有時候時間是以 hh:mm:ss 的格式輸入的。
是否有其他方法可以解決這個問題?請參閱範例 v2。
祝好,C
PS:下次見到你我請你喝咖啡或啤酒!
回覆:如何修復缺少冒號的持續時間:如果這是來自真實資料(不同格式的字元列)的話?如果我沒記錯的話,原始範例是數值型資料。秒數是否始終為 00,就像範例中一樣?
回覆:如何修復缺少冒號的持續時間:是的,很遺憾,這是來自真實數據。
我選擇了兩個沒有這個問題的列,因為我以為可以用重新編碼的方式解決。
但我錯了,或者至少我意識到對 84 列使用 recode 對我來說負擔太重了。
是的,秒數始終為 00。
回覆:如何修復缺少冒號的持續時間:當列為字元列時,可以使用快速公式方法計算公式。此公式假設已格式化的持續時間值會在小時部分填入零(例如0 9:00:00)。
numonly = Substitute(_relative_from_X__1, ":", ""); // remove ":"
hrm = Left(numonly, 4); // take 4 characters from left
val = Num(hrm); // convert to numeric for calculations
h = Floor(val / 100); // take only hundreds
m = Mod(val, 100); // remainder
t = h * 60*60 + m * 60; // convert to seconds

我在這裡創建新列,以便輕鬆地將它們與初始值進行比較。建立公式後,使用標準化屬性(資料類型、建模類型、格式)進行必要的類型變更。

看更多...
This will create new data columns, you might want to use in place data instead
// Transform column
Data Table("example for JMP com v2") << Apply Formula(
Columns(Group(:X__1, 12), Ref("_relative_from_X__1")),
Formula(
numonly = Substitute(_relative_from_X__1, ":", "");
hrm = Left(numonly, 4);
val = Num(hrm);
h = Floor(val / 100);
m = Mod(val, 100);
t = h * 60 * 60 + m * 60;
),
Output(New Static)
) << Run Formulas;
// Standardize column attributes
Local({old dt = Current Data Table()},
Current Data Table(Data Table("example for JMP com v2"));
For Each({col, index},
{:X__1 2, :X__2 2, :X__3 2, :X__4 2, :X__5 2, :X__6 2, :X__7 2, :X__8 2,
:X__9 2, :X__10 2, :X__11 2, :X__12 2},
col << Set Data Type(Numeric, Format("Best", 12))
);
Current Data Table(old dt);
);
// Standardize column attributes
Local({old dt = Current Data Table()},
Current Data Table(Data Table("example for JMP com v2"));
For Each({col, index},
{:X__1 2, :X__2 2, :X__3 2, :X__4 2, :X__5 2, :X__6 2, :X__7 2, :X__8 2,
:X__9 2, :X__10 2, :X__11 2, :X__12 2},
col << Set Modeling Type("Continuous")
);
Current Data Table(old dt);
);
// Standardize column attributes
Local({old dt = Current Data Table()},
Current Data Table(Data Table("example for JMP com v2"));
For Each({col, index},
{:X__1 2, :X__2 2, :X__3 2, :X__4 2, :X__5 2, :X__6 2, :X__7 2, :X__8 2,
:X__9 2, :X__10 2, :X__11 2, :X__12 2},
col << Format("hr:m", 12)
);
Current Data Table(old dt);
);