I have data like these:
col1 col2 col3 col4 col5
1 3 1 7 3
4 2 8 2 5
3 1 5 1 4
I want to add columns that show the minimum and maximum by row, but only for certain columns (2 - 4, for example):
col1 col2 col3 col4 col5 min max
1 3 1 7 3 1 7
1 2 8 2 5 2 8
9 1 5 1 0 1 5
I know I could use select to subset those rows and then calculate the min/max and use cbind to merge with the original data, but I feel like there is a better approach. Thanks!
Data
df <- structure(list(col1 = c(1L, 4L, 3L), col2 = 3:1, col3 = c(1L, 8L, 5L),
col4 = c(7L, 2L, 1L), col5 = c(3L, 5L, 4L)),
class = "data.frame", row.names = c(NA, -3L))