I have a data frame in R
called records
. Here is an extract of that dataframe:
head(records[430:438,12:16])
2B2_pyr_hor 2B3_pyr_hor 3A1_pyr_hor 3A2_pyr_hor 3A3_pyr_hor
3760 22.45732 21.63635 24.36742 20.11058 28.10608
3761 33.81917 31.96332 36.45907 29.76239 41.94274
3762 47.38274 44.32909 51.10520 41.49609 57.54125
3763 62.47752 58.20380 67.02381 54.71055 74.55541
3764 78.17504 73.55406 83.88879 69.30802 92.57984
3905 90.84730 94.72380 89.43837 106.69484 81.44858
I want to extract all the rows where all the values are <100
but when I do records=records[records<100,]
, it seems I get all the rows where at least one value is less <100
.
So in this example, I do not want row with index=3905
yet I still get it. (The above extract is after I applied records=records[records<100,]
)
Nothing in my first past searches have produced any useful solutions unfortunately. It must be something simple that I am missing somewhere?
We can use rowSums
df[rowSums(df < 100) == ncol(df), ]
#Alternative version :
#df[rowSums(df >= 100) == 0, ]
# 2B2_pyr_hor 2B3_pyr_hor 3A1_pyr_hor 3A2_pyr_hor 3A3_pyr_hor
#3760 22.5 21.6 24.4 20.1 28.1
#3761 33.8 32.0 36.5 29.8 41.9
#3762 47.4 44.3 51.1 41.5 57.5
#3763 62.5 58.2 67.0 54.7 74.6
#3764 78.2 73.6 83.9 69.3 92.6
Or with lapply
and Reduce
df[Reduce(`&`, lapply(df, `<`, 100)), ]
data
df <- structure(list(`2B2_pyr_hor` = c(22.45732, 33.81917, 47.38274,
62.47752, 78.17504, 90.8473), `2B3_pyr_hor` = c(21.63635, 31.96332,
44.32909, 58.2038, 73.55406, 94.7238), `3A1_pyr_hor` = c(24.36742,
36.45907, 51.1052, 67.02381, 83.88879, 89.43837), `3A2_pyr_hor` = c(20.11058,
29.76239, 41.49609, 54.71055, 69.30802, 106.69484), `3A3_pyr_hor` = c(28.10608,
41.94274, 57.54125, 74.55541, 92.57984, 81.44858)), class = "data.frame",
row.names = c("3760","3761", "3762", "3763", "3764", "3905"))
You can use apply
and all
to select rows where all values are below 100 .
records[apply(records<100, 1, all),]
# X2B2_pyr_hor X2B3_pyr_hor X3A1_pyr_hor X3A2_pyr_hor X3A3_pyr_hor
#3760 22.45732 21.63635 24.36742 20.11058 28.10608
#3761 33.81917 31.96332 36.45907 29.76239 41.94274
#3762 47.38274 44.32909 51.10520 41.49609 57.54125
#3763 62.47752 58.20380 67.02381 54.71055 74.55541
#3764 78.17504 73.55406 83.88879 69.30802 92.57984
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.