I have this dataframe:
df <- data.frame(
column_names = c("x1", "x1", "x2", "x2")
,row_names = c("y1", "y2", "y1", "y2")
,n = c(1,2,3,4)
)
and would like to transform it into a matrix like this:
x1 x2
y2 2 4
y1 1 3
How can I achieve this please?
You can use data.table::dcast
to reshape the data, then remove the row_names
column, set the rownames, and order the rows by rownames descending.
out <- data.table::dcast(df, row_names ~ column_names, value.var = 'n')
out_mat <- as.matrix(out[, -1])
rownames(out_mat) <- out$row_names
out_mat <- out_mat[order(rownames(out_mat), decreasing = T),]
out_mat
# x1 x2
# y2 2 4
# y1 1 3
Or with tidyverse
library(tidyverse)
df %>%
spread(column_names, n) %>%
arrange(desc(row_names)) %>%
column_to_rownames('row_names') %>%
as.matrix
# x1 x2
# y2 2 4
# y1 1 3
You can do this with tidyr
:
library(tidyr)
df <- spread(df, column_names, n)
df <- df[order(df$row_names, decreasing = TRUE),]
Edited for decreasing y values.
This will give you the exact output you asked for (a matrix
with row and column names, arranged by descending y
):
library(dplyr)
mtx <- df %>%
group_by(row_names) %>%
arrange(column_names) %>%
summarise(out=list(n)) %>%
arrange(desc(row_names)) %>%
pull(out) %>%
do.call('rbind', .)
rownames(mtx) <- df %>%
distinct(row_names) %>%
arrange(desc(row_names)) %>%
pull(row_names)
colnames(mtx) <- df %>%
distinct(column_names) %>%
arrange(column_names) %>%
pull(column_names)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.