【问题标题】:Error message in R: replacement has (x) rows, data has (y)R中的错误消息:替换有(x)行,数据有(y)
【发布时间】:2018-09-03 11:53:01
【问题描述】:

我在尝试更改列中数据元素的名称时收到错误消息。这是我正在使用的数据框的结构。

'data.frame':   2070 obs. of  7 variables:
 $ Period: Factor w/ 188 levels "1 v 1","10 v 10",..: 158 158 158 158 158 158 158 158 158 158 ...
 $ Dist  : num  7548 7421 9891 8769 10575 ...
 $ HIR   : num  2676 2286 3299 2455 3465 ...
 $ V6    : num  66.2 18.5 81 40 275.1 ...
 $ Date  : Factor w/ 107 levels "1/3/17","1/4/17",..: 38 38 38 38 38 38 38 38 38 38 ...
 $ Type  : Factor w/ 28 levels "Captain's Run",..: 5 5 5 5 5 5 5 5 5 5 ...
 $ Day   : Factor w/ 8 levels "Friday","Monday",..: 1 1 1 1 1 1 1 1 1 1 ...
#> Error: <text>:1:22: unexpected symbol
#> 1: 'data.frame':   2070 obs.
#>                          ^
```

我希望将db$Type 中的值Main Session 更改为Main Training,这样我就可以将此数据框与我正在使用的另一个数据框相匹配。我正在使用下面的代码来尝试这样做。

class(db$Type)
db$Type <- as.character(db$Type)
db$Type["Main Session"] = "Main Training"

当我尝试运行这段代码时收到此错误消息。

db$Type["Main Session"] = "Main Training"
Error in `$<-.data.frame`(`*tmp*`, Type, value = c("Main Session", "Main Session",  : 
  replacement has 2071 rows, data has 2070
#> Error: <text>:2:7: unexpected 'in'
#> 1: db$Type["Main Session"] = "Main Training"
#> 2: Error in
#>          ^

对于 R 来说相对较新,我的代码中是否缺少可以解决此问题的任何内容?任何建议将不胜感激。谢谢。

【问题讨论】:

  • 感谢您尝试的详细错误消息和代码。如果您从数据中抽取一小部分样本并使用dput() 生成一些代码,这样其他人可以简单地将其粘贴到他们的控制台中并重现您的数据,这将非常有帮助。 see here 同时也许你可以试试db$Type[db$Type == "Main Session"] = "Main Training"
  • 感谢您的帮助,@Dean。
  • 不用担心@B.Horsley。我希望这能解决问题。如果您想了解更多详细信息,我会扩展我的评论作为答案。

标签: r


【解决方案1】:

您遇到的错误与您的子集操作有关:db$Type["Main Session"] = "Main Training"

使用 R 中的mtcars 数据集,我们可以重现此错误:

str(iris)
#> 'data.frame':    150 obs. of  5 variables:
#>  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#>  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#>  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#>  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#>  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
class(iris$Species)
#> [1] "factor"
iris$Species<- as.character(iris$Species)
iris$Species["setosa"] <- "new name"
#> Error in `$<-.data.frame`(`*tmp*`, Species, value = structure(c("setosa", : replacement has 151 rows, data has 150

reprex package (v0.2.0) 于 2018 年 9 月 3 日创建。

在方括号内,您需要使用逻辑运算(即计算结果为 TRUE 或 FALSE 的运算)对向量进行子集化。

str(iris)
#> 'data.frame':    150 obs. of  5 variables:
#>  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#>  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#>  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#>  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#>  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
iris$Species<- as.character(iris$Species)
unique(iris$Species)
#> [1] "setosa"     "versicolor" "virginica"
iris$Species[iris$Species == "setosa"] <- "new name"
unique(iris$Species)
#> [1] "new name"   "versicolor" "virginica"

reprex package (v0.2.0) 于 2018 年 9 月 3 日创建。

【讨论】:

    猜你喜欢
    • 2018-02-19
    • 2015-07-01
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多