【发布时间】:2020-03-19 17:47:58
【问题描述】:
我正在尝试将单个列中的所有零条目替换为 0.001。我尝试了以下方法:
data7[data7$Time == 0,] <- 0.001
这给了我以下错误:
Error in as.POSIXct.numeric(value) : 'origin' must be supplied
【问题讨论】:
标签: r
我正在尝试将单个列中的所有零条目替换为 0.001。我尝试了以下方法:
data7[data7$Time == 0,] <- 0.001
这给了我以下错误:
Error in as.POSIXct.numeric(value) : 'origin' must be supplied
【问题讨论】:
标签: r
如果。它是单列,我们需要
data7$Time[data7$Time == 0] <- 0.001
在 OP 的代码中,它通过指定 , 替换数据集的所有列
【讨论】:
并且,作为对@akrun 答案的补充,为了完整起见,如果您在示例中省略逗号,它将替换数据框中的所有零:
data(iris)
x <- iris[1:10, ]
x$Sepal.Length[sample(1:nrow(x), 5)] <- 0
x$Sepal.Width[sample(1:nrow(x), 5)] <- 0
x$Petal.Length[sample(1:nrow(x), 5)] <- 0
x
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 0.0 3.5 1.4 0.2 setosa
#> 2 4.9 0.0 0.0 0.2 setosa
#> 3 0.0 3.2 1.3 0.2 setosa
#> 4 0.0 3.1 0.0 0.2 setosa
#> 5 5.0 0.0 1.4 0.2 setosa
#> 6 5.4 0.0 0.0 0.4 setosa
#> 7 4.6 0.0 0.0 0.3 setosa
#> 8 0.0 3.4 1.5 0.2 setosa
#> 9 4.4 2.9 0.0 0.2 setosa
#> 10 0.0 0.0 1.5 0.1 setosa
x[x == 0] <- Inf
x
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 Inf 3.5 1.4 0.2 setosa
#> 2 4.9 Inf Inf 0.2 setosa
#> 3 Inf 3.2 1.3 0.2 setosa
#> 4 Inf 3.1 Inf 0.2 setosa
#> 5 5.0 Inf 1.4 0.2 setosa
#> 6 5.4 Inf Inf 0.4 setosa
#> 7 4.6 Inf Inf 0.3 setosa
#> 8 Inf 3.4 1.5 0.2 setosa
#> 9 4.4 2.9 Inf 0.2 setosa
#> 10 Inf Inf 1.5 0.1 setosa
由reprex package (v0.3.0) 于 2020 年 3 月 19 日创建
【讨论】: