Hugo HK
Hugo HK

Reputation: 63

R getting error of number of rows when using replace function

I'm trying to get rid of some specific words in a data frame column. So the data set looks like somewhat like this with 3235 rows:

V1                            V2
AUTAUGA COUNTY                1
BALDWIN COUNTY                3
VALDEZ-CORDOVA CENSUS AREA    261

what I'm trying to do is:

data$V1 <- replace(data$V1, " COUNTY", "")

But I get an error that looks like this:

Error in `$<-.data.frame`(`*tmp*`, "V1", value = c("AUTAUGA COUNTY",  : 
   replacement has 3236 rows, data has 3235

Am I using the function the wrong way? Or is there any other way to do this? Thanks!

Upvotes: 0

Views: 646

Answers (1)

ebyerly
ebyerly

Reputation: 672

Hugo, For the example you've provided, this code works well:

eg <- data.frame(V1 = c("AUTUAGA COUNTY", "BALDWIN COUNTY",
                        "VALDEZ-CORDOVA CENSUS AREA"),
                 V2 = c(1, 3, 261))
eg$gsub <- gsub(" COUNTY", "", eg$V1)
eg
-                           V1  V2                       gsub
- 1             AUTUAGA COUNTY   1                    AUTUAGA
- 2             BALDWIN COUNTY   3                    BALDWIN
- 3 VALDEZ-CORDOVA CENSUS AREA 261 VALDEZ-CORDOVA CENSUS AREA

Does this resolve the error?

(Edited to fix the output column names.)

Upvotes: 2

Related Questions