简体   繁体   中英

Remove all rows below a certain threshold in R

fmcountdata1 表 I have a matrix of data and I wanted to remove all rows that have values below a set threshold of 10. I've checked other posts on here and they don't seem to work in my case in R for some reason. I still relatively new to R so getting to grips with it at the moment. What would you recommend I do to accomplish this?

For example, I would want the row "MIR6859-1" removed completely as it has count data below 10 across every condition.

Here is the code I have tried so far but I keep getting the error "Error in data < 10 : comparison (3) is possible only for atomic and list types"

or that the column name "KOA1" object not found with the subset method.

enter code here fmcountdata1 <- mergecountdata1[!(mergecountdata1$KOA1<10),] enter code here fmcountdata1 <- enter code here mergecountdata1[!apply(data<10,1,any,na.rm=TRUE),] enter code here fmcountdata1 <- mergecountdata1 enter code here subset(fmcountdata1, KOA1<10)

Here is a snippet of the dataset:

KOA1 KOA2 KOA3 KOA4 KOB1 KOB2 KOB3 KOB4 CON1 CON2 CON3 CON4 DDX11L1 0 0 0 0 0 0 0 0 0 0 0 0 WASH7P 16 28 25 54 28 26 21 40 17 30 19 39 MIR6859-1 4 1 1 3 1 0 0 0 0 1 0 1 MIR1302-2HG 0 1 0 1 1 0 0 1 0 0 0 0 MIR1302-2 0 0 0 0 0 0

str of my data set: chr [1:59412, 1:12] " 0" " 16" " 4" " 0" ... - attr( , "dimnames")=List of 2 ..$ : chr [1:59412] "DDX11L1" "WASH7P" "MIR6859-1" "MIR1302-2HG" ... ..$ : chr [1:12] "KOA1" "KOA2" "KOA3" "KOA4" ... - attr( , "names")= chr [1:712944] NA NA NA NA ...

Is this what you had in mind?

set.seed(12)
data <- data.frame(v1=sample(c(1:20,NA), 10),
                   v2=sample(c(1:20,NA), 10))
data
   v1 v2
1   2 18
2  16  6
3  14 12
4   5 10
5  18  7
6  12 16
7  NA 13
8   8  8
9  11  4
10 15 14

# Remove rows of data if *any* column in that row contains a value<10
data.any <- data[!apply(data<10,1,any,na.rm=TRUE),]
data.any # rows 3,6,7 and 10 remain
   v1 v2
3  14 12
6  12 16
7  NA 13
10 15 14

# Remove rows of data if *all* columns in that row contains a value<10
data.all <- data[!apply(data<10,1,all,na.rm=TRUE),]
data.all # all but row 8 remain
   v1 v2
1   2 18
2  16  6
3  14 12
4   5 10
5  18  7
6  12 16
7  NA 13
9  11  4
10 15 14

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