New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spread with missing values of a factor of key_value #25

Closed
MCOtto opened this Issue Sep 6, 2014 · 4 comments

Comments

Projects
None yet
3 participants
@MCOtto

MCOtto commented Sep 6, 2014

I want to make tables from different data where I use spread to make tables with levels of a factor for columns. spread is a good tool to do this. I need all columns even if there are no observations of some levels of the factor. reshape::cast with add.missing accomplished this; spread has no option with the same ability. Here is a simple example:

require(tidyr)
 require(dplyr)
 Data<-expand.grid(
    row=paste("r",1:3,sep=""),
    col=paste("c",1:4,sep="")   
 )

 Data<-mutate(Data,
    value=as.integer(gsub("[rc]","",paste(row,col,sep="")))
 )

 filter(Data,col!="c3")%>%
 spread(col,value,fill=0)

 require(reshape)
 filter(Data,col!="c3")%>%
 cast(row~col,
  add.missing=TRUE,value="value",fill=0
 )

cast keeps c3 even though there are not values because levels(Data$col) has all the columns. Spread does not:

row c1 c2 c3 c4
1 r1 11 12 0 14
2 r2 21 22 0 24
3 r3 31 32 0 34

This is the facility from cast I would like retained or returned. May be I am missing something in the spread options or some other function I should be using in tid- or dpl-yr.

@desmondpatel

This comment has been minimized.

desmondpatel commented Sep 7, 2014

Is tidyr available for R version 3.03?

@MCOtto

This comment has been minimized.

MCOtto commented Sep 7, 2014

This does not address this issue for spread, but you look in the description file for the package.

Depends: R (>= 3.1.0)

means that it requires a version later than 3.03. I would update R.

Mark

@MCOtto MCOtto closed this Sep 7, 2014

@MCOtto MCOtto reopened this Sep 7, 2014

@hadley

This comment has been minimized.

Member

hadley commented Oct 7, 2014

MRE:

require(tidyr)
require(dplyr)

df <- expand.grid(
    row = paste("r", 1:3, sep = ""),
    col = paste("c", 1:4, sep = "")   
  ) %>% mutate(
    value = paste0(rep(1:3, each = 4), 1:4)
  ) %>%
  filter(col != "c3")

df %>% spread(col, value, fill = 0)
df %>% reshape2::dcast(row ~ col, drop = FALSE, fill = 0)

@hadley hadley closed this in 2833fb3 Oct 7, 2014

@hadley

This comment has been minimized.

Member

hadley commented Oct 7, 2014

I've called the argument drop, since that what it is in reshape2 (and it's consistent with other uses)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment