Thursday, 15 March 2012

r - dplyr cross tab with missing values -


I want to create a cross-tab from R to dplyr . I just have good reasons not to use the base table () command.

  table (mtcars $ cyl, mtcars $ gear) 3 4 5 4 1 8 2 6 2 4 1 8 12 0 Library (DEPUTOR) Library (TDER) MTRR% & gt;% by Group_ (Cyl, gear)% & gt;% (%)% & gt;% spread (gear, n, fill = 0) source: local data frame [3 x 4] cyl 3 4 5 1 4 1 8 2 2 6 2 4 1 3 8 12 0 2  

It is all good and good, but it seems that when values ​​in the group_by () variables do not disappear, since then Separate.

  mtcars%>% mutate (cyl = ifelse (cyl> 6 (cal, gear)% & gt;% clue () Source: Local data frame [8 x 3] Group: CIL, NA, CIL, Gear = Afhel (Gear> 4, NA, Gear)% Cal Gear N 1 4 3 1 2 4 4 8 3 4 NA 2 4 6 3 2 5 6 4 4 6 6 NA 1 7 NA 3 12 8 NA NA 2 # Error #%>% Error in transmission (Gear, N) (if any (name 2 (x) == "")) {: unavailable value where TRUE / FALSE I need what I want  NA  when you use the  table (..., useNA = "always"), such as columns. Any Also suggest?   

Change an option NA with the label. It can be easily done with mutate_each :

  mtcars%>% mutate (cyl = ifelse (cyl> 6, n, cyl), gear = ifelse (Gear & gt; 4, NA, gear))% & gt;% by Group_ (COB, Gear)% & gt;% OFF ()% & gt;% Non-Group ()% & gt;% mutate_each (Functional (replacement (., Is.na (.), 'Missing'))%)% spread (gear, n) # cal3 4 unavailable # 1 4 1 8 2 # 2 6 2 4 1 # 3 missing 12 NA2  

No comments:

Post a Comment