triplet <- data.frame(i = as.integer(c(9, 7, 5, 9, 7, 5)),
j = as.integer(c(3, 3, 3, 4, 4, 4)),
value = c(1, 2, 3, 4, 5, 6)) |>
setrowtype("rows") |> setcoltype("cols")
triplet
rowtype(triplet)
coltype(triplet)
# We have more indices than actual entries in the martix
r_indices <- data.frame(names = paste0("r", 1:101),
indices = 1:101)
head(r_indices)
c_indices <- data.frame(names = paste0("c", 1:101),
indices = 1:101)
head(c_indices)
# Names are interpreted as row and column types
indices <- list(cols = c_indices, rows = r_indices)
named <- to_named_matrix(triplet, indices)
named
triplet2 <- to_triplet(named, indices)
# Although not in the same row order,
# triplet and triplet2 are the same.
triplet2
rowtype(triplet2)
coltype(triplet2)
# And the same matrix can be recovered from triplet2
to_named_matrix(triplet2, indices)
Run the code above in your browser using DataLab