0

I am new to shinyR and I would like to convert the values in a column of an existing data frame into row names and the values in a column into col names . Something like this data:

   Date             Type  Count 
  23 May 2005        A     2
  24 May 2005        B     1 
  25 May 2005        D     3  
  26 May 2005        D     3   
  26 May 2005        A     3   

Into this :

   23 May 2005   24 May 2005  25 May 2005   26 May 2005 
 A        2          0             0             3
 B        0          1             1             0
 Other    0          0             3             3 

I tried :

vcol <- length(data$date)
table119 <- matrix(data$count, ncol=vcol,byrow=TRUE)
rownames(table119) <- data$type
colnames(table119) <- data$date
table_stathas <- - DT::renderDataTable({table119()})

but it is not working

It is different from Convert the values in a column into row names in an existing data frame in R , I want to change the colnames and the rownames with the value of the columns Date and Type that are not uniques

rcd92
  • 105
  • 2
  • 9
  • @KoenV not really,I the value of the column Type are not distinct and so is the the value from the column Date that I want to make as colnames – rcd92 Sep 05 '18 at 08:43
  • Understood. I will remove the `flag`. – KoenV Sep 05 '18 at 08:50

1 Answers1

0

A base R option would be

df$Type[!df$Type %in% c("A", "B")] <- "Other"
xtabs(Count ~ Type + Date, df) 
akrun
  • 874,273
  • 37
  • 540
  • 662