Say I have something like this
df <- data.frame(row1 = c(1, 2), row2 = c(3, 1), row3 = c(1, 4))
and I want to collapse the columns keeping only the maximum value from each row such that df is a single row containing 2, 3, 4. How can I do this?
Say I have something like this
df <- data.frame(row1 = c(1, 2), row2 = c(3, 1), row3 = c(1, 4))
and I want to collapse the columns keeping only the maximum value from each row such that df is a single row containing 2, 3, 4. How can I do this?
With data.table
:
library(data.table)
setDT(df)[, lapply(.SD, max)]
Result:
row1 row2 row3
1: 2 3 4
Using dplyr
df %>% dplyr::summarise_all(max)
# row1 row2 row3
# 1 2 3 4