Here's an approach that gets you your desired output:
tcrossprod(table(mydf))
# user_id
# user_id 98461 311542 875740 24376261
# 98461 3 0 1 0
# 311542 0 1 0 0
# 875740 1 0 1 0
# 24376261 0 0 0 1
(tcrossprod(table(mydf)) != 0) + 0
# user_id
# user_id 98461 311542 875740 24376261
# 98461 1 0 1 0
# 311542 0 1 0 0
# 875740 1 0 1 0
# 24376261 0 0 0 1
If you want the result as a data.frame
, you can wrap the output in as.data.frame.matrix
.
This is assuming that "mydf" is defined as:
mydf <- structure(list(user_id = c(24376261L, 98461L, 98461L, 98461L,
311542L, 875740L), post_id = c(204506440L, 204446324L, 203026202L,
203031838L, 204351465L, 203031838L)), .Names = c("user_id", "post_id"),
class = "data.frame", row.names = c(NA, -6L))