0

I am getting an error message when I am attempting to change the name of a data element in a column. This is the structure of the data frame I am using.

'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.
#>                          ^
```

I wish to change the value Main Session in db$Type to Main Training so I can match this data frame to another I'm using. I'm using the code below to try and do this.

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

I am getting this error message when I attempt to run the piece of code.

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
#>          ^

Being relatively new to R, is there anything I am missing in my code that could resolve this issue? Any suggestions will be greatly appreciated. Thank you.

Mr. T
  • 11,960
  • 10
  • 32
  • 54
  • Thanks for the detailed error message and code you tried. It would really help if you took a small sample of your data and used `dput()` to generate some code so others could simply paste this in their console and reproduce your data. [see here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) In the meantime perhaps you could try `db$Type[db$Type == "Main Session"] = "Main Training"` – Dean Sep 02 '18 at 22:55
  • Thanks for your help, @Dean. –  Sep 02 '18 at 23:03
  • No worries @B.Horsley. I hope this did the trick. I expanded on my comment as an answer if you wanted more details. – Dean Sep 03 '18 at 00:32

1 Answers1

1

The error you are encountering is in relation to your subset operation: db$Type["Main Session"] = "Main Training".

Using the mtcars dataset in R we can reproduce this error:

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

Created on 2018-09-03 by the reprex package (v0.2.0).

Inside the square brackets you need to subset the vector using a logical operation (i.e. one that evaluates to TRUE or 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"

Created on 2018-09-03 by the reprex package (v0.2.0).

Dean
  • 479
  • 3
  • 9