0

I'm having a problem in R.

I have some data where I have different regions. Below is an example and it's actual names not values.

D1$Regions = c(Ab, Ba, Da-Cd, Db-a, Da-Aa)

If I only want to use the region Da-Cd and trying to select that one only I get an error.

D-C<-D1[D1$Regions =="Da-Cd",]

I get following this error:

Error in D - C <- D1[D1$Region == "Da-Cd", ] : 
  object 'Da' not found

I assume it's because it's trying to subtract C from D, but in this case the actual name of the region is D-C. What can I do to only select that region? Can I do it without having to rename the region?

In my data I have several regions with a "-" between letters. It'll be fine if the "-" is removed for all the different regions.

I have tried to use D1$Region as character and as factor but that doesn't help.

Thanks.

Sotos
  • 51,121
  • 6
  • 32
  • 66
  • 1
    [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making a reproducible example. Unless you have objects called e.g. `Da-Cd`, the first line isn't valid R syntax; either way, it's unclear exactly what you're working with – camille Feb 13 '20 at 14:50

2 Answers2

2

- shouldn't be used in a name since it is reserved for the subtraction operator; it is reserved/illegal. But you could get around it by surrounding the name with illegal symbols with ``, although in some (all?) contexts the more familiar "" will suffice.

`D-C` <- D1[D1$Regions =="Da-Cd",]
s_baldur
  • 29,441
  • 4
  • 36
  • 69
1

R reads Da-Cd as Da minus Cd. I'd suggest to use characters as names for regions, i.e. c("Ab", "Ba", "Da-Cd", "Db-a", "Da-Aa")

Wolfgang Arnold
  • 1,252
  • 8
  • 17