Search code examples
rdcast

Dcast() weird output


I have two dataframes. Applying the same dcast() function to the two get me different results in the output. Both the dataset have the same structure but different size. The first one has more than 950 rows:

enter image description here

The code I apply is:

trans_matrix_complete <- mod_attrib$transition_matrix




trans_matrix_complete[which(trans_matrix_complete$channel_from=="_3RDLIVE"),]

trans_matrix_complete <- rbind(trans_matrix_complete, df_dummy) 


trans_matrix_complete$channel_to <- factor(trans_matrix_complete$channel_to,
                             levels = c(levels(trans_matrix_complete$channel_to)))

trans_matrix_complete <- dcast(trans_matrix_complete,
                               channel_from ~ channel_to,value.var = 'transition_probability')

And the trans_matrix_complete output I get is the following:

enter image description here

Something is not working as it should be as with the smaller dataframe of just few lines I get the following outcome:

enter image description here

Where

a) the row number is different. I'm not sure why there are two dots listed in the first case

b) and too, trying to assign rownames to the dataframe by

row.names(trans_matrix_complete) <- trans_matrix_complete$channel_from

does not work for the large dataframe, as despite the row.names contact the dataframe show up exactly as in the first image, without names assigned to rows.

Any idea about this weird behavior?


Solution

  • I resolved moving from dcast() to spread() of the package tidyverse using the following function:

    trans_matrix_complete<-spread(trans_matrix_complete, channel_to,transition_probability)

    By applying spread() the two dataframe the matrix output is of the same format and accept rownames without any issue.

    So I suspect it is all realted to the fact that dcast() and reshape2 package are not maintained anymore

    Regards