0

I am new in R. So please help me to figure out the following problem.

My data are as follows.

> CheckIn    - ID 
> 2016/Jan/1 - 100 
> 2016/Feb/1 - 100
> 2014/Jan/1 - 100 
> 2014/Jan/1 - 101 
> 2015/Feb/1 - 100

I want to figure out following tables from above data

     Jan - Feb - Mar 
2014  2  -  0  - 0
2015  0  -  1  - 0
2016  1  -  1  - 0

Is it possible in R?

Iqbal
  • 235
  • 8
  • 20

2 Answers2

1

We can try

library(data.table)
setDT(df1)[, c("Year", "Month") := {dt <- as.Date(CheckIn, "%Y/%b/%d"); list(format(dt, "%Y"), format(dt, "%b"))}]
dcast(df1, Year~Month, value.var="ID", length)
akrun
  • 874,273
  • 37
  • 540
  • 662
0

You could try the cast function from the reshape package

library("reshape") 
format the data in order to be properly stored in a data table
my_data=data.table(CheckIn=c("2016/Jan/1","2016/Feb/1","2014/Jan/1","2014/Jan/1","2015/Feb/1"),ID=c("100","100","100","101","100"))   
my_data[,"Year":=substr(CheckIn,1,4)]
my_data[,"Month":=substr(CheckIn,6,8)]
my_data[,"Frequency":=as.numeric(substring(CheckIn,10))] 
use cast to get the result you want
cast(my_data,Year~Month,value="Frequency")