I have this matrix:
row c1 c2 c3 c4 c5
R1 27 38 94 40 4
R2 69 16 85 2 15
R3 30 35 64 95 6
R4 20 33 77 98 55
R5 20 44 60 33 89
R6 12 88 87 44 38
I want to delete all columns in the matrix that have an average of less than 30. How is this done with colMeans?
We can use colMeans (assuming the row is the row.names of the matrix)
m1[, colMeans(m1, na.rm = TRUE) >= 30, drop = FALSE]
-output
c2 c3 c4 c5
R1 38 94 40 4
R2 16 85 2 15
R3 35 64 95 6
R4 33 77 98 55
R5 44 60 33 89
R6 88 87 44 38
if row is a column, then it is not making sense as a matrix because a matrix can have only a single type and thus any character element changes the whole matrix to character. Probably a data.frame would be better i.e.
df1[, c(TRUE, colMeans(df1[-1], na.rm = TRUE) >= 30), drop = FALSE]
-output
row c2 c3 c4 c5
1 R1 38 94 40 4
2 R2 16 85 2 15
3 R3 35 64 95 6
4 R4 33 77 98 55
5 R5 44 60 33 89
6 R6 88 87 44 38
m1 <- structure(c(27L, 69L, 30L, 20L, 20L, 12L, 38L, 16L, 35L, 33L,
44L, 88L, 94L, 85L, 64L, 77L, 60L, 87L, 40L, 2L, 95L, 98L, 33L,
44L, 4L, 15L, 6L, 55L, 89L, 38L), .Dim = 6:5, .Dimnames = list(
c("R1", "R2", "R3", "R4", "R5", "R6"), c("c1", "c2", "c3",
"c4", "c5")))
df1 <- structure(list(row = c("R1", "R2", "R3", "R4", "R5", "R6"), c1 = c(27L,
69L, 30L, 20L, 20L, 12L), c2 = c(38L, 16L, 35L, 33L, 44L, 88L
), c3 = c(94L, 85L, 64L, 77L, 60L, 87L), c4 = c(40L, 2L, 95L,
98L, 33L, 44L), c5 = c(4L, 15L, 6L, 55L, 89L, 38L)),
class = "data.frame", row.names = c(NA,
-6L))
If we change matrix to dataframe we could use select with condition: (data borrowed from akrun)
df1 %>%
select(which(colMeans(.) >= 30))
output:
c2 c3 c4 c5
R1 38 94 40 4
R2 16 85 2 15
R3 35 64 95 6
R4 33 77 98 55
R5 44 60 33 89
R6 88 87 44 38
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With