In the below example data frame myDF
, I am trying to figure out the base R or dplyr code to derive the number of groupings of elements whereby Group code <> 0. For example in this data frame, with the generating code beneath and dput()
version at the very bottom, I would like to get two different outputs:
- a single digit output of 4 (where Elements R with Group = 1, X with Group = 1, R with Group 2, and X with Group -3, comprise the 4 groupings), and
- an output show only those grouped elements (listing out those grouped elements listed in the above bullet).
Any recommendations for a simple and concise way to do this?
> myDF
Element Group
1 R 1
2 R 1
3 X 0
4 X 1
5 X 1
6 X 0
7 B 0
8 R 0
9 R 2
10 R 2
11 X -3
12 X -3
13 X -3
Generating code:
myDF <- data.frame(
Element = c("R","R","X","X","X","X","B","R","R","R","X","X","X"),
Group = c(1,1,0,1,1,0,0,0,2,2,-3,-3,-3)
)
Dput(myDF):
> dput(myDF)
structure(list(Element = c("R", "R", "X", "X", "X", "X", "B",
"R", "R", "R", "X", "X", "X"), Group = c(1, 1, 0, 1, 1, 0, 0,
0, 2, 2, -3, -3, -3)), class = "data.frame", row.names = c(NA,
-13L))
CodePudding user response:
In dplyr
:
filter(myDF, Group !=0) %>% distinct()
gives
Element Group
1 R 1
2 X 1
3 R 2
4 X -3