简体   繁体   中英

How to select the column of a matrix based on increasing numbers

I have a matrix with 4 columns (below example)

   a  b c   d
1 10 85 0 115
2 15 74 0 125
3 18 65 0 130
4 20 64 0 150

I want to select just a column whose number increases along the length of the column (A & D column).

   A   D
1 10 115
2 15 125
3 18 130
4 20 150
df<-data.frame(A=c(10,15,18,20),B=c(85,74,65,64),
               C=c(0,0,0,0),D=c(115,125,130,150))

Note

My actual data is huge and this example is a test to explain better.

For strictly increasing

> names(which(colMeans(sapply(df,diff)>0)==1))
[1] "a" "d"

using this on the data frame

> df[,colMeans(sapply(df,diff)>0)==1]
   a   d
1 10 115
2 15 125
3 18 130
4 20 150

There is an inbuilt function is.unsorted for this purpose which checks if a vector is sorted without the cost of actually sorting it. So this should work faster on large datasets.

strictly_increasing <- function(x) !is.unsorted(x, strictly = TRUE)

df[sapply(df, strictly_increasing)]

#   A   D
#1 10 115
#2 15 125
#3 18 130
#4 20 150

You may also use it with dplyr -

library(dplyr)

df %>% select(where(strictly_increasing))

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM