0

I've read through others who have had a similar issue, but my situation doesn't seem to be the same as the fixes that have been proposed for those other issues. I'm trying to recode a variable using a conditional statement. I want to take a character string & turn it into a numeric so I can subset those observations out into a new data frame. Here's what I have, so far:

blad_mor <- read.csv("blad_mor.csv", header = T)

str(blad_mor)

blad_mor_recode <- gsub(C670:C679, 29010, blad_mor$cod)

I get this output for the str() command:

> str(blad_mor)
'data.frame':   127073 obs. of  12 variables:
 $ year          : int  1999 1999 1999 1999 1999 1999 1999 1999 1999 1999 ...
 $ sex           : Factor w/ 4 levels "1","2","F","M": 1 1 1 2 1 2 2 2 2 2 ...
 $ race          : Factor w/ 17 levels "America","Asian &",..: 4 4 4 4 4 4 4 4 4 4 ...
 $ county        : Factor w/ 79 levels "COUNTY1","COUNTY2",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ cod           : Factor w/ 327 levels "C001","C005",..: 89 108 108 294 63 42 172 74 85 269 ...
 $ fips          : int  1 1 1 1 1 1 1 1 1 1 ...
 $ state         : int  5 5 5 5 5 5 5 5 5 5 ...
 $ race_code     : int  2 2 2 2 2 2 2 2 2 2 ...
 $ ethnicity     : Factor w/ 4 levels "","Hispanic",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ ethnicity_code: int  NA NA NA NA NA NA NA NA NA NA ...

But when I try the blad_mor_recode <- gsub(C670:C679, 29010, blad_mor$cod) code I get this error:

> blad_mor_recode <- gsub(C670:C679, 29010, blad_mor$cod)
Error in gsub(C670:C679, 29010, blad_mor$cod) : object 'C670' not found

So, I verify that there actually is that object by table(blad_mor$cod) with this being some of the output:

C578  C579   C58   C60  C601  C609   C61  C629  C631  C639   C64   C65   C66  C670  C672  C674  C675  C676 
    2    43     4     1     1    53  6162    62     1    14  2911    30    47    1     4     1     1     2 
 C677  C678  C679  C680  C689  C690  C692  C693  C694  C695  C696  C699  C700  C701  C709   C71  C710  C711 
    1     4  2776    35    77     1     4     5     1     1     8    45     7     3    11     1    29    34 

The object 'C670' has one instance as per this output, yet R is telling me it is not there & doesn't run the command. What am I missing here? Should I change the class type from factor to something else? I'm quite confused.

Edit: I have tried quotes around the character strings (e.g. blad_mor_recode <- gsub('C670:C679', '29010', blad_mor$cod) as well as ifelse(). I still get the same error message.

jhines
  • 9
  • 5
  • 3
    You are actually feeding `gsub()` a nonexisting object `C670` instead of a character string `C670` – boski Sep 25 '19 at 15:00
  • 1
    What are you trying to do? The first parameter to `gsub` should be a regular expression pattern. You seem to be trying to pass a list of column names or something? It's very unclear what you are attempting. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Use `dput()` rather than `str()` to share data. – MrFlick Sep 25 '19 at 15:00
  • What do you think `C670:C679` means in R? Did you try to run it outside the `gsub` function? – nicola Sep 25 '19 at 15:07
  • It seems very unlikely that `blad_mor_recode <- gsub('C670:C679', '29010', blad_mor$cod)` would return the exact same error message. Also, what exactly did your `ifelse` attempt look like? You really should start form the beginning and state what problem you are trying to solve rather than just asking why a code chunk doesn't work. There may be better ways to solve it than with the functions you have chosen. – MrFlick Sep 25 '19 at 15:16

1 Answers1

1

If you want to change all strings from C70to C79 you have to use regex. Something like the following would work:

blad_mor_recode <- gsub("C7[0-9]", "29010", blad_mor$cod)

A simple example:

gsub("C7[0-9]","",c("C60","C70","C78"))
[1] "C60" ""    "" 
boski
  • 2,437
  • 1
  • 14
  • 30
  • This worked. Of course it was something simple. Thanks for the response! – jhines Sep 25 '19 at 15:48
  • no problem :) if you believe this answers your question, mark it as the accepted answer so people know its been answered. Cheers. – boski Sep 25 '19 at 15:49