xtabs from the R base works fine for this problem:
dat <- read.table(text="person week year personA 6 1 personA 22 1 personA 41 1 personA 42 1 personA 1 2 personA 23 2 personB 8 2 personB 9 2 ", header=TRUE) xtabs(~person+year, data=dat) #----------------- year person 1 2 personA 4 2 personB 0 2
You can pass your output to matplot, as it returns a table / matrix object:
matplot( xtabs(~person+year, data=dat))
The output x-axis in this small example may not be what you want, but with a lot of years, there may be a more satisfactory default marking. Or you can suppress the default xaxt = "n" labels on the x axis and use axis to label as you wish:
matplot( xtabs(~person+year, data=dat), xaxt="n", type="b")
42-
source share