jack petersen
jack petersen

Reputation: 1

Function to use "-" as text

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.

Upvotes: 0

Views: 72

Answers (2)

s_baldur
s_baldur

Reputation: 33488

- 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",]

Upvotes: 2

Wolfgang Arnold
Wolfgang Arnold

Reputation: 1252

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")

Upvotes: 1

Related Questions