Select() function in dplyr package

Hello, I've used select() as before, but it give me an error. I've also tried to use reprex to debug, but there is also a confused error. Thus, I turn to community for help.

The simplified data sets I used are:
g1
ID Age Gender HDL.g1 YZ.g1 HPX.g1 Status.g1
1 1 11 m 45.3 23.4 1 1
2 2 12 f 44.5 34.1 2 3
3 3 13 f 43.7 44.8 2 5
4 4 14 f 42.9 55.5 1 4
5 5 15 f 42.1 66.2 2 2
6 6 16 m 41.3 76.9 2 4
7 7 17 f 40.5 87.6 1 1
8 8 18 m 39.7 98.3 2 3
9 9 19 m 38.9 109.0 1 4
10 10 20 m 38.1 119.7 2 2
11 11 21 f 37.3 130.4 1 4
12 12 22 f 36.5 141.1 2 1
13 13 23 f 35.7 151.8 1 1
14 14 24 m 34.9 162.5 2 3
15 15 25 f 34.1 173.2 2 5
16 16 26 m 33.3 183.9 1 4
17 17 27 m 32.5 194.6 2 3
18 18 28 m 31.7 205.3 2 3
19 19 29 m 30.9 216.0 1 5
20 20 30 m 23.0 226.7 2 4
21 21 31 m 32.0 237.4 1 3

g2
ID Age Gender A.g2 B.g2 C.g2
1 18 16 m d -1 55.5
2 19 17 f c 0 66.2
3 20 18 m a 1 76.9
4 21 19 m b 1 87.6
5 22 24 m b 1 98.3
6 6 16 m a 1 109.0
7 7 17 f c 1 119.7
8 8 18 m a 1 44.5
9 9 19 m a -1 43.7
10 10 20 m c 0 42.9
11 11 21 f d 1 42.1
12 12 22 f a 1 41.3
13 13 23 f b 1 40.5
14 14 24 m b 1 39.7
15 15 25 f a 1 38.9
16 16 26 m c -1 38.1
17 17 27 m d 0 37.3

The following is my code:

m1 <- merge(x=g1,y=g2,by="ID",all = TRUE)
  m2 <- merge(x=g1,y=g2,by="ID",all = FALSE)
  
  matched_id = m2$ID  # matched id between 2 groups
  matched_id_num = length(m2$ID)
  num_unmatched_id = nrow(m1) - nrow(m2) # unmatched id between 2 groups
  
  m2_x <- select(m2, ends_with(".x"))
  m2_x <- cbind(m2[,"ID"],m2_x)
  m2_y <- select(m2, ends_with(".y"))
  m2_y <- cbind(m2[,"ID"],m2_y)

The error is:

Error in select(m2, ends_with(".x")) : unused argument (ends_with(".x"))

I really have no idea to fix it. Hope anyone can help me:sob::sob::sob:

1 Like

Hello Ivy - chances are you have another package attached that also has a select function and R thinks you are calling that. This is called masking and you will see a warning when loading the packages. Try using:

m2_x <- dplyr::select(m2, ends_with(".x"))

the package::function notation tells R to call the function function from the package package.

9 Likes

Yeah!!!!! Your solution really help. Thank you very much!!:cake:

You can control masking by attaching the package with the versions you want (here dplyr) last, so they're earlier on the search() path. Or if you're like me, just never actually attach MASS and call everything from it prefixed with MASS::.

3 Likes

If your question's been answered, would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

P.S. Since the problem was not related to the RStudio IDE, I've moved this thread to a different category.

1 Like

true - also, since it is often hard to identify conflicts deep in code long after attaching packages, another approach is to make use of the conflicted package which forces you to explicitly choose the function via the package::function notation for any function with a conflict.

https://www.tidyverse.org/articles/2018/06/conflicted/

2 Likes