R counts two column values ​​and gets the result as a table

I have a dataset as shown

Num              Day
1111             Thursday
2222             Thursday
3333             Thursday
1111             Monday
1111             Tuesday
1111             Wednesday
1111             Friday
1111             Saturday
1111             Sunday
2222             Thursday

I need to get each counter Numfor the corresponding one Day. I used as.data.frame(table(data$Num,data$Day))and got the output, as in the following format.

Num              Day              Freq
1111             Thursday         1
1111             Monday           1
1111             Tuesday          1
1111             Wednesday        1
1111             Friday           1
1111             Saturday         1
1111             Sunday           1
.
.
2222             Thursday         2
.
.
3333             Thursday         1
.
.

For better visualization, I need the output as shown below.

      Sunday  Monday  Tuesday  Wednesday  Thursday  Friday  Saturday
1111  1       1       1        1          1         1       1
2222  0       0       0        0          2         0       0
3333  0       0       0        0          1         0       0 

Is there any way to do this?

+4
source share
1 answer

try it

table(data)
      Day
Num    Friday Monday Saturday Sunday Thursday Tuesday Wednesday
  1111      1      1        1      1        1       1         1
  2222      0      0        0      0        2       0         0
  3333      0      0        0      0        1       0         0
+4
source

All Articles