0

I know the c function is a generic function which combines its arguments. The problem that I encountered is about the c function with more than 2 characters in r. The question and example as below:

Step 1: I create a data frame with 2 columns Region and Salary.

> x<-data.frame(Region=c("a","b","c","c","a","b"),Salary=c(11:16))
> x
  Region Salary
1      a     11
2      b     12
3      c     13
4      c     14
5      a     15
6      b     16

Step 2: I can get the correct answer when I chose one region(EX:a or b or c).

> x[x$Region=="a",]
  Region Salary
1      a     11
5      a     15
> x[x$Region=="c",]
  Region Salary
3      c     13
4      c     14

Step 3: If I choose the multiple region (EX:(a,b),(a,c),(b,c),(a,b,c)), the result would be unpredictable.

x[x$Region==c("a","c"),]
  Region Salary
1      a     11
4      c     14
5      a     15

Question: I thought the c function is "combine" its arguments. So the result of x[x$Region==c("a","c"),] would be the same as the result of x[x$Region=="a" | x$Region=="c",]. But it turns out only show the row 1,4,5...Where is row 3??

What does x[x$Region==c("a","c"),] mean??

Thanks for your time

fallsit
  • 11
  • 1
  • 6
  • you need `?match` – Cath Jul 09 '18 at 11:48
  • Hi Cath, I know I can use %in% to find the answer. I just want to know what does c("a","c") means? Is it mean "a" & "c" or "a" | "c" or it means nothing or it has other meaning? Thanks~~ – fallsit Jul 09 '18 at 12:57
  • it means a vector whose first element is "a" and second element is "c" – Cath Jul 09 '18 at 13:06
  • 1
    Thanks Cath. I suddenly got the point !! It doesn't matter what is c("a","c"). The point is I should use %in% instead of "==". Thanks again Cath~ – fallsit Jul 09 '18 at 13:18

0 Answers0