出现错误“要替换的项目数不是替换长度的倍数". [英] Getting an error "number of items to replace is not a multiple of replacement length"

查看:144
本文介绍了出现错误“要替换的项目数不是替换长度的倍数".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用strptime函数将记录转换为日期和时间格式.但是,我不确定为什么会出现错误:

I'm trying to convert a record into a date and time format using the strptime function. However, I'm not sure why I'm getting the error:

要替换的项目数不是替换长度的倍数.

number of items to replace is not a multiple of replacement length.

我尝试使用length函数检查记录的长度,但是两者的长度相同.

I tried to check the length of the record using the length function but both have the same length.

data <- DT
head(data[6])
#                column
# 1 2014-12-22 23:53:48
# 2 2014-12-22 23:20:34
# 3 2014-12-22 23:20:30
# 4 2014-12-22 23:20:16
# 5 2014-12-22 23:20:07
# 6 2014-12-22 23:05:49

data[,6] <- as.character(data[,6])

temp_file <- matrix(0,nrow=nrow(data))

temp_file[1] <- strptime(data[1, 6],"%F %T")
# Warning message:
# In temp_file[1] <- strptime(data[1, 6], "%F %T") :
#   number of items to replace is not a multiple of replacement length

length(temp_file[1])
# [1] 1

length(data[1,6])
# [1] 1

length(strptime(data[1, 6], "%F %T") )
# [1] 1

非常感谢您的帮助.

谢谢!

推荐答案

您可以使用lubridate包的ymd_hms函数将字符向量转换为日期时间格式:

You can transform character vector into datetime format using ymd_hms function of lubridate package:

library(lubridate)

# data frame simulation
structure(list(X1 = c(1, 1, 1, 1, 1, 1), X1.1 = c(1, 1, 1, 1, 1, 1), 
    X1.2 = c(1, 1, 1, 1, 1, 1), X1.3 = c(1, 1, 1, 1, 1, 1), 
    X1.4 = c(1, 1, 1, 1, 1, 1), date_time_char = c("2014-12-22 23:53:48", 
    "2014-12-22 23:20:34", "2014-12-22 23:20:30", "2014-12-22 23:20:16", 
    "2014-12-22 23:20:07", "2014-12-22 23:05:49")), class = "data.frame", row.names = c(NA, -6L))

# transform from character to datetime
data$date_time <- ymd_hms(data[, 6])
data[, 7]

输出:

[1] "2014-12-22 23:53:48 UTC" "2014-12-22 23:20:34 UTC" "2014-12-22 23:20:30 UTC" "2014-12-22 23:20:16 UTC"
[5] "2014-12-22 23:20:07 UTC" "2014-12-22 23:05:49 UTC"

大卫·阿伦堡(David Arenburg)的评论非常好:

N.B. Really nice comment by David Arenburg:

这实际上是一个好问题.这不是错误,而是 警告,但您得到的结果是错误的,因此您可以将其视为 一个错误.发生这种情况的原因是由于 R中的一个矩阵,该矩阵只能获得原子向量.当你尝试的时候 将strptime传递给矩阵,它的类是"POSIXlt""POSIXt",因此 它对其进行取消分类,并因此返回其属性的列表(其中 长度大于1),即unclass(s​​trptime(data [1,1],%F%T")). 第一个值是48秒.这正是您所拥有的 temp_file [1].

That is actually a good question. This isn't an error, rather a warning, but the result you get is wrong, so you could consider it as an error. The reason this is happening is because of the definition of a matrix in R, which can only get atomic vectors. When you are trying to pass strptime to the matrix, it's class is "POSIXlt" "POSIXt", thus it is unclasses it and thus returns a list of its attributes (which length is larger than 1), i.e., unclass(strptime(data[1,1],"%F %T")). The first value is 48 seconds. This is exactly what you have in temp_file[1] now.

这篇关于出现错误“要替换的项目数不是替换长度的倍数".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆