0

I have a data.frame directly readed from an archive with this information:

    ID  date    consumption 
1   X1  19/02/2014  0
2   X1  19/05/2014  0
3   X1  20/08/2014  11
4   X1  18/11/2014  4
5   X2  19/02/2014  1
6   X2  19/06/2014  8
7   X2  20/08/2014  9
8   X3  18/11/2014  11

And i want to create a data.frame like this one:

             X1   X2   X3
19/02/2014    0    1   NA
19/05/2014    0   NA   NA
20/08/2014   11    9   NA
18/11/2014    4   NA   11
19/06/2014   NA    8   NA

with the dates as rownames and the ID as colnames. I've do it with the following code but i'm sure that must be another approach more efficient.

IDlist=mydataframe[!duplicated(mydataframe[,"ID"],),"ID"]
Datelist=mydataframe[!duplicated(mydataframe[,"date"],),"date"]

newdf=data.frame(matrix(NA, nrow = length(Datelist), ncol = length(IDlist)))  
rownames(newdf)=Datelist
colnames(newdf)=IDlist
for(i in 1:nrow(mydataframe)){
     mydataframe[newdf[i,"date"],newdf[i,"ID"]]=newdf[i,"consumption"]
}  

Please, could anyone help me to do it more efficient? I'm pretty new programming in R.

  • Hint: look at the reshape-package, with the dcast function. Then you can do `dcast(date~ID,value.var="consumption", data=mydataframe)` – Heroka Dec 10 '15 at 14:34
  • 1
    or tidyr or base R reshape or data.table dcast .... or ... just look at some other SO question. – Colonel Beauvel Dec 10 '15 at 14:41
  • Many thanks to everybody. dcast works quite good with some sample test that i've done but with my code I don't know why said that "value.var (consumption) not found in input". mydataframe is defined obviously as a data.frame with colnames ID, date & consumption. Any idea about what could be happening? – Javier Orgaz Dec 11 '15 at 08:09
  • Problem solved. mydataframe was defined initially as data.frame but later it was transformed. So "mydataframe= as.data.frame(mydataframe)" was the solution. Thanks once again. – Javier Orgaz Dec 11 '15 at 09:14

0 Answers0