2

I would like to assign different group number for each pairs of rows. and for some pairs assigning unique numbers as a group number.

edit

We can think of those are exist in the data as pairs. If those pairs exist in the rows, assign a group number to them until the next pairs. Since there might be other data rows in the real data.

Here is the example data

 names <- c(c("bad","good"),c("good","bad"),c("bad","J.James"),c("Good","J.James"),c("J.James","Good"),c('Veni',"vidi","Vici"))

  df <- data.frame(names)

      names
1      bad
2     good
3     good
4      bad
5      bad
6  J.James
7     Good
8  J.James
9  J.James
10    Good
11    Veni
12    vidi
13    Vici

as I concentrated each pairs such as c("bad","good") would like to group them and for pairs c('Veni',"vidi","Vici") assign unique number 666.

So the expected output

     names Group
1      bad     1
2     good     1
3     good     2
4      bad     2
5      bad     3
6  J.James     3
7     Good     4
8  J.James     4
9  J.James     5
10    Good     5
11    Veni     666
12    vidi     666
13    Vici     666

I would appreciate your help on this.

I've also posted a more complicated case as a new question at the suggestion of commenters.

Frank
  • 63,401
  • 8
  • 85
  • 161
Alexander
  • 3,691
  • 5
  • 30
  • 66

1 Answers1

4

You can try something like the following:

x <- c('Veni',"vidi","Vici")
library(data.table)
setDT(df)[, Group := ((sequence(nrow(df))-1) %/% 2)+1][names %in% x, Group := 666][]
#       names Group
#  1:     bad     1
#  2:    good     1
#  3:    good     2
#  4:     bad     2
#  5:     bad     3
#  6: J.James     3
#  7:    Good     4
#  8: J.James     4
#  9: J.James     5
# 10:    Good     5
# 11:    Veni   666
# 12:    vidi   666
# 13:    Vici   666
A5C1D2H2I1M1N2O1R2T1
  • 177,446
  • 27
  • 370
  • 450
  • And the solution is your general ? What if there are other rows after c(bad,good,1,2,3) for example. – Alexander Feb 21 '18 at 18:33
  • 1
    @Alexander Re dplyr: `df %>% mutate(g = ((sequence(nrow(df))-1) %/% 2)+1, g = replace(g, names %in% x, 666))`. Re "what about" -- you should provide an example that covers the cases you care about, maybe as a new question... – Frank Feb 21 '18 at 18:35
  • I've got to say, if you want to accomplish this task, you should be looking at putting your data into lists beforehand, instead of using `data.frame()` or `data.table()`. Lists look like a better way of approaching this, despite Franks and A5's answers (which are good). – InfiniteFlash Feb 21 '18 at 18:35
  • 1
    @Alexander, I think you're gong to have to give an example of what you mean by "general". – A5C1D2H2I1M1N2O1R2T1 Feb 21 '18 at 18:37
  • @A5C1D2H2I1M1N2O1R2T1 Please see the second edit part. Thanks for your help! – Alexander Feb 21 '18 at 18:43
  • 1
    @Frank Please see the second edit part. I explained about the 'what about' part. – Alexander Feb 21 '18 at 18:43
  • @A5C1D2H2I1M1N2O1R2T1 You right I opened a [new post](https://stackoverflow.com/questions/48913362/special-grouping-number-for-each-pairs) – Alexander Feb 21 '18 at 18:52
  • @Alexander Please consider to accept this post as the answer since the post can generate the desired output of your original question. – www Feb 21 '18 at 19:14