-2

I have a data with two factors: industry and time.

industry    time
1        1990
1        1990
2        1990
2        1991
3        1990
3        1990

I want to create a factor A that put observations with the same industry and time into each levels:

industry    time      A
1        1990      1
1        1990      1
2        1990      2
2        1991      3
3        1990      4
3        1990      4
A. Suliman
  • 12,923
  • 5
  • 24
  • 37
1412mm
  • 55
  • 7
  • 2
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. This will make it easier to help you. – MrFlick Aug 23 '18 at 20:18

2 Answers2

1

If you don't particularly care what the number actually is in column A, then you can use

transform(dd, A=as.numeric(interaction(industry, time)))

Or if it doesn't actually need to be a number and a factor will do, then just

transform(dd, A=interaction(industry, time))

Tested with the data in

dd <-read.table(text="industry    time
1        1990
1        1990
2        1990
2        1991
3        1990
3        1990", header=TRUE)
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • `interaction()` is a better approach than my `merge()` or `factor(paste())` approaches. I'll delete mine. – DanY Aug 23 '18 at 20:39
0

group_by will get all unique combinations of industry and time. Using row_number we can exclude the duplicate combination for taking into account (Only cumsum if A==1)

library(dplyr)
df %>% group_by(industry,time) %>% mutate(A=row_number()) %>% 
       ungroup() %>% mutate(B=cumsum(A==1))

# A tibble: 6 x 4
     industry  time     A     B
        <int> <int> <dbl> <dbl>
  1        1  1990     1     1
  2        1  1990     2     1
  3        2  1990     1     2
  4        2  1991     1     3
  5        3  1990     1     4
  6        3  1990     2     4
A. Suliman
  • 12,923
  • 5
  • 24
  • 37