【问题标题】:R warning: "number of items to replace is not a multiple of replacement length" seems incorrectR警告:“要替换的项目数不是替换长度的倍数”似乎不正确
【发布时间】:2017-11-08 00:53:13
【问题描述】:

我有一个如下所示的示例 R 脚本:

# Create example data
date <- c("11/09/2016", "11/02/2016", "11/16/2016", "11/23/2016")
column_two <- c(4, 2, 3, 4)
# Populate a data frame and make sure the dates have the correct class
mydata <- data.frame(date, column_two)
mydata$date <- strptime(mydata$date, format="%m/%d/%Y")

print("The contents of mydata are:")
print(mydata)

# Create a dummy list (or vector, or array, or what is it?)
foo <- rep(NA, 5)
print("foo is initialized to:")
print(foo)
print("The class of foo is:")
print(class(foo))

earlydate <- min(mydata$date)
print(sprintf("Earliest date is: %s", earlydate))
print("The class of earlydate is:")
print(class(earlydate))
print(sprintf("Length of earliest date is: %d", length(earlydate)))
print(sprintf("Length of foo[2] is: %d", length(foo[2])))

# Attempt to set one variable equal to another
foo[2] <- earlydate

print("After assignment, foo looks like this:")
print(foo)
print("Now the classes of foo, foo[2], and foo[[2]] are:")
print(class(foo))
print(class(foo[2]))
print(class(foo[[2]]))

脚本的打印输出如下所示:

> source("test_warning.R")
[1] "The contents of mydata are:"
        date column_two
1 2016-11-09          4
2 2016-11-02          2
3 2016-11-16          3
4 2016-11-23          4
[1] "foo is initialized to:"
[1] NA NA NA NA NA
[1] "The class of foo is:"
[1] "logical"
[1] "Earliest date is: 2016-11-02"
[1] "The class of earlydate is:"
[1] "POSIXlt" "POSIXt" 
[1] "Length of earliest date is: 1"
[1] "Length of foo[2] is: 1"
[1] "After assignment, foo looks like this:"
[[1]]
[1] NA

[[2]]
[1] 0

[[3]]
[1] NA

[[4]]
[1] NA

[[5]]
[1] NA

[1] "Now the classes of foo, foo[2], and foo[[2]] are:"
[1] "list"
[1] "list"
[1] "numeric"
Warning message:
In foo[2] <- earlydate :
  number of items to replace is not a multiple of replacement length
> 

我有很多问题:

  • foo[2]earlydate 的长度明显相同时,为什么我会收到警告?
  • 为什么 foo[2] 的值设置为0 而不是earlydate 的值?
  • 很明显,R 自动将foo 变量及其元素强制转换为新类;为什么这些类(foo,或foo[2],或foo[[2]])似乎与earlydate 变量的类("POSIXlt" "POSIXt")不匹配?
  • 如何正确初始化foo,使其成为一个向量、列表或类似数组的东西,其中包含能够支持这种类型的值分配的各个元素,而不会引发警告并导致这种类型的计数器- 直觉行为?

【问题讨论】:

    标签: arrays r list vector variable-assignment


    【解决方案1】:

    嗯,POSIXlt 的幕后其实是一个列表。

    > class(unclass(earlydate))
    [1] "list"
    > length(unclass(earlydate))
    [1] 11
    

    赋值取 0,因为这是列表的第一个元素;它是秒数,earlydate 为 0。

    > unclass(earlydate)[1]
    $sec
    [1] 0
    

    我真的不知道为什么 R 不会自动将 foo 变量强制转换为 POSIXlt 类;我的猜测是,强制约会通常很难。很清楚在所有foo 都是NA 的情况下在这里做什么,但是如果其中一个元素已经是整数或字符串怎么办?但要先自己强制,请使用as.POSIXlt

    foo <- rep(as.POSIXlt(NA), 5)
    

    根据您还打算用它做什么,您可能还会发现使用POSIXct 是更好的解决方案,正如Integer to POSIXlt 的回答中所讨论的那样。它将日期存储为自原始以来的秒数,因此不是下面的列表,因此更适合某些类型的计算和存储。

    【讨论】:

      猜你喜欢
      • 2020-02-02
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 2014-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多