I have a dataframe in long format (https://docs.google.com/spreadsheets/d/15jDW1pCYs7VD6MAH9GHmYrP-zf8WjsGj9F3qVXGijDM/edit?usp=sharing) that looks like this:
objectid timestamp code_bbch_surveyed
1: 702509 2018-03-23 NA
2: 702509 2018-03-23 NA
3: 702509 2018-03-23 NA
4: 702509 2018-03-23 NA
5: 702509 2018-03-23 NA
---
5581: 293171 2018-10-17 GMA3
5582: 293171 2018-10-17 GMA3
5583: 293171 2018-10-17 GMA3
5584: 293171 2018-10-17 GMA3
5585: 293171 2018-10-17 GMA3
I want to cast it to wide format so that every row is a unique objectid
, every column is a unique timestamp
and the cells are populated by the respective code_bbch_surveyed
.
I've tried what seems the most logical implementation of dcast
like so:
dcast(setDT(df_scr), objectid ~ timestamp, value.var = 'code_bbch_surveyed')
but this produces an output dataframe/datatable where each cell is populated by the COUNT/number of instances. I wish to NOT count the instances but simply populated the cell with the value in code_bbch_surveyed
.
So instead of the output like this (row1) :
objectid 2018-03-23 2018-04-23 2018-05-21 2018-06-20 2018-07-09 2018-08-15 2018-09-20 2018-10-17
1: 8100 27 22 16 14 15 14 12 15
I would like to see an output like this row(1):
objectid 2018-03-23 2018-04-23 2018-05-21 2018-06-20 2018-07-09 2018-08-15 2018-09-20 2018-10-17
1: 8100 SCR2 WWH3 [null] [null] [null] [null] [null] [null]