I am trying to get the structure where the Location is the unique row, Lab is the unique Row, and the account is inside the matrix.
I have tried tables and have not been to get it 100% correct.
End Result:
sample:
structure(list(Location = c(1001, 1001, 1001, 1002, 1002, 1002,
1003, 1003), Lab = c(1, 2, 3, 1, 2, 3, 1, 2), Account = c(53127,
28724, 23646, 53128, 28725, 23647, 53130, 28727)), row.names = c(NA,
-8L), class = c("tbl_df", "tbl", "data.frame"))
If we need a matrix
, then use xtabs
from base R
xtabs(Account ~ Location + Lab, df1)
Or consider pivot_wider
from tidyr
, and then convert to matrix
library(tidyr)
library(tibble)
library(dplyr)
df1 %>%
pivot_wider(names_from = Lab, values_from = Account) %>%
column_to_rownames('Location') %>%
as.matrix