Getting an error “number of items to replace is not a multiple of replacement length”

六月ゝ 毕业季﹏ 提交于 2020-01-14 07:52:21

问题


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.

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

Any help is greatly appreciated.

Thanks!


回答1:


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]

Output:

[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"

N.B. Really nice comment by David Arenburg:

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.



来源:https://stackoverflow.com/questions/27820213/getting-an-error-number-of-items-to-replace-is-not-a-multiple-of-replacement-le

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!