0
我有以下的玩具data.table
[R data.table轉換日期列到一個月
x = data.table(dt = c('2015-01-01','2015-02-01','2016-04-01'))
我想一個額外的列添加到具有最新的三個字母縮寫表。我試過
x$dt = strptime(x$dt,'%b')
但它給了下面的警告和空白字段。
Warning messages:
1: In `[<-.data.table`(x, j = name, value = value) :
Supplied 9 items to be assigned to 3 items of column 'dt' (6 unused)
2: In `[<-.data.table`(x, j = name, value = value) :
Coerced 'list' RHS to 'character' to match the column's type. Either change the target column to 'list' first (by creating a new 'list' vector length 3 (nrows of entire table) and assign that; i.e. 'replace' column), or coerce RHS to 'character' (e.g. 1L, NA_[real|integer]_, as.*, etc) to make your intent clear and for speed. Or, set the column type correctly up front when you create the table and stick to it, please.
> x
dt mnth
1: c(NA, NA, NA)
2: c(NA, NA, NA)
3: c(NA, NA, NA)
我發現之前Q &一個上,這樣說,strptime
返回列表,但一直沒能完成這個任務相對簡單。有人能提供一些指針嗎?
沒有在本例中沒有 'mnth'。如果'dt'是列,'format(strptime(x $ dt,'%Y-%m-%d'),'%b')' – akrun
更正/編輯問題。您的解決方案適用於@akrun。謝謝。 – broccoli
或:'x [,mnth:= months(strptime(dt,'%Y-%m-%d'))]' – Jaap