0

次のおもちゃを持っています。data.table

 x = data.table(dt = c('2015-01-01','2015-02-01','2016-04-01'))

日付の 3 文字の省略形を持つ列をテーブルに追加しようとしています。私は試した

 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)     

strptimeリストを返したが、この比較的単純なタスクを達成できなかったという SO に関する以前の Q&A を見つけました。誰かがいくつかの指針を提供できますか?

4

1 に答える 1

3

使用できますformat

format(strptime(x$dt, '%Y-%m-%d'), '%b')

または@Frankが述べたように、クラスformat.Dateに適用できますDate

format(as.Date(x$dt), "%b")
于 2016-02-20T18:39:51.163 に答える