See the chat transcript before voting to close please
I have the following data:
> dput(head(q,10))
structure(list(Date = structure(c(1471424400, 1471424400, 1471424400,
1471424401, 1471424401, 1471424406, 1471424407, 1471424415, 1471424417,
1471424514), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
Type = c("ASK", "BID", "ASK", "BID", "ASK", "ASK", "BID",
"BID", "BID", "ASK"), Price = c(1749.95, 1611, 1683.9, 1653,
1672, 1683.9, 1653, 1654.2, 1663, 1682)), .Names = c("Date",
"Type", "Price"), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
Then I used the following code:
data.new <- group_by(head(q,10), Date, Type) %>% summarize(price=ifelse(Type[1] == 'ASK', min(Price)
Then used
dputed<-dput(ungroup(data.new))
dput(dputed)
structure(list(Date = structure(c(1471424400, 1471424400, 1471424401,
1471424401, 1471424406, 1471424407, 1471424415, 1471424417, 1471424514
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), Type = c("ASK",
"BID", "ASK", "BID", "ASK", "BID", "BID", "BID", "ASK"), price = c(1683.9,
1611, 1672, 1653, 1683.9, 1653, 1654.2, 1663, 1682)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -9L), .Names = c("Date",
"Type", "price"))
I want to convert it into wide format, where Date, Ask, and Bid are three columns and rows contain both ask and bid vlaues for a particular time stamp. This is what I have tried:
m.q<-dcast(dputed,Date ~ Type, value.var = "price")
But the result have duplicate time stamps. Please notice the time stamp (2016-08-17 09:00:06) in two adjacent rows. Due to which bid and aks are not in the same row:
> dput(m.q)
structure(list(Date = structure(c(1471424400, 1471424401, 1471424406,
1471424407, 1471424415, 1471424417, 1471424514), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), ASK = c(1683.9, 1672, 1683.9, NA,
NA, NA, 1682), BID = c(1611, 1653, NA, 1653, 1654.2, 1663, NA
)), .Names = c("Date", "ASK", "BID"), row.names = c(NA, -7L), class = "data.frame")
Please notice when I use complete.cases() I get only 2 rows instead of 3 as 2016-08-17 09:00:06 get deleted because bid and ask values get bifurcated into two rows with the same time stamp.
johny<- m.q[complete.cases(m.q),]
> dput(johny)
structure(list(Date = structure(c(1471424400, 1471424401), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), ASK = c(1683.9, 1672), BID = c(1611,
1653)), .Names = c("Date", "ASK", "BID"), row.names = 1:2, class = "data.frame")