Reputation: 31
#Simulated data /model output
data1 <-data.frame(col1=c(10,20,30,40,50,60,70,80,90,100),
col2=c(2,4,6,8,10,12,14,16,20,22),
col3=c(3,9,12,15,18,21,24,27,30,33),
col4= c(4,8,12,16,20,24,28,32,36,40))
#Lower 95% CI from real data
lowdata <- as.data.frame(matrix(0,1,4))
lowdata[,1] <-5
lowdata[,2] <-34
lowdata[,3] <-25
lowdata[,4] <-30
# Higher 95% CI from real data
highdata <-as.data.frame(matrix(0,1,4))
highdata[,1] <- 59
highdata[,2] <- 60
highdata[,3] <- 50
highdata[,4] <- 49
I am new to R programming and not sure how to do the following.
I want to basically find out which ones of the data1 rows have values (for each column) that are within the Cis defined by two the two dataframes (lowdata1 and highdata1).
I have tried this, but it takes each value and not row:
wmax <- which(data1[,1:4] < highdata1[,1:4])
wmin <- which(data1[,1:4] > lowdata1[,1:4])
w <- intersect(wmax, wmin)
How could I achieve extracting which rows in data1 basically "fit" between the CIs?
Upvotes: 2
Views: 53
Reputation: 28675
You can check whether each element of each column is in the corresponding interval with
is.btwn <-
Map(function(x, low, high) x >= low & x <= high,
data1, lowdata, highdata)
is.btwn
# $col1
# [1] TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
#
# $col2
# [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#
# $col3
# [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE
#
# $col4
# [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE
You can use Reduce
to get rows where all columns are in the interval, but in this case there are no such rows
Reduce(`&`, is.btwn)
# [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Or the rows with any column in the corresponding interval
Reduce(`|`, is.btwn)
# [1] TRUE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE
All of these results can be passed to which
to get indices with value TRUE
Upvotes: 1