【发布时间】:2022-01-12 23:15:06
【问题描述】:
我在 R 中使用 Ionosphere 数据集,并尝试编写一个循环来创建新列,这些新列是现有列的标准化迭代并相应地命名它们。
我将“cname”作为新列名,将 c 作为原始列名。代码是:
install.packages("mlbench")
library(mlbench)
data('Ionosphere')
library(robustHD)
col <- colnames(Ionosphere)
for (c in col[1:length(col)-1]){
cname <- paste(c,"Std")
Ionosphere$cname <- standardize(Ionosphere$c)
}
但是得到以下错误:
"Error in `$<-.data.frame`(`*tmp*`, "cname", value = numeric(0)) :
replacement has 0 rows, data has 351
In addition: Warning message:
In mean.default(x) : argument is not numeric or logical: returning NA"
我觉得我错过了一些超级简单的东西,但我就是看不到它。
感谢您的帮助。
【问题讨论】:
-
避免使用常见的函数名称命名对象通常是一种好习惯,例如
c(如c())。你确定有一个名为“c”的变量吗? -
请分享这个“电离层”数据。你可以使用
dput(head(Ionosphere, 10)) -
c 基本上是循环。它遍历每个列名,然后(理论上)使用原始列名 + std 创建一个新列。
-
data_set是什么? -
抱歉,data_set 不应该在那里。正在尝试不同的事情。我现在已经编辑过了。