Hi All,
I am somewhat new to R and I have run into a point where I need some help. I figure the reshape package can accomplish what I need to do.
Here is the structure of the original data frame:
> str(bruins)
'data.frame': 10 obs. of 6 variables:
$ gameid : Factor w/ 1 level "20090049": 1 1 1 1 1 1 1 1 1 1
$ team : chr "NYI" "BOS" "NYI" "BOS" ...
$ home_ind: chr "V" "H" "V" "H" ...
$ period : Factor w/ 5 levels "1","2","3","4",..: 1 1 2 2 3 3 4 4 5 5
$ goals : int 0 0 3 0 0 3 0 0 3 3
$ shots : int 16 7 9 7 8 12 5 4 38 30
Here are the first few rows:
> head(bruins)
gameid team home_ind period goals shots
409 20090049 NYI V 1 0 16
410 20090049 BOS H 1 0 7
411 20090049 NYI V 2 3 9
412 20090049 BOS H 2 0 7
413 20090049 NYI V 3 0 8
414 20090049 BOS H 3 3 12
I am looking to create a new data frame that pivots on gameid and period, with the rest of the columns summarizing the data for each home_ind row (10 columns in all).
When I run the following code:
b.melt <- melt(bruins, id=c("gameid", "period"), na.rm=TRUE)
I get the following error:
Warning messages:
1: In `[<-.factor`(`*tmp*`, ri, value = c(0L, 0L, 3L, 0L, 0L, 3L, 0L, :
invalid factor level, NAs generated
2: In `[<-.factor`(`*tmp*`, ri, value = c(16L, 7L, 9L, 7L, 8L, 12L, :
invalid factor level, NAs generated
Any help will be very much appreciated!
Edit: This is what I am hoping to get the restructured data to look like
gameid period vis_team vis_goals vis_shots home_team home_goals home_shots
1 20090049 1 NYI 0 16 BOS 0 7
2 20090049 2 NYI 3 9 BOS 0 7
3 20090049 3 NYI 0 8 BOS 3 12