Reputation: 3678
I have a dataframe df
with a column named x1
with values between -5 and +5. I am trying to assign for each row of df
an interval regarding the values of x1
. The function cut
allow me do to so :
cut(df$x1,c(-5,-4,-3,-2,-1,0,1,2,3,4,5))
and I can then split df
into 10 data.frames
using by
. Unfortunately what I would like is to assign intervals like -5 to -3.95, -4.05 to -2.95, -3.05 to -1.95 and so on, meaning that :
which means that after using by
I will have 10 dataframes with a few elements in 2 of those dataframes.
The next part of my question would concern the values near 0 : the intervals should not contain negative and positive values, so the intervals would be like
Is there a way to achieve that in R ?
EDIT : df
df
looks like this :
other_var ... x1 ... another_var ...
100 ... 4 ... 18 ...
12.3 ... 3.84 ... -6.2 ...
1.4 ... 4.78 ... 4.78 ...
-2 ... -2.51 ... 7.1 ...
-3.2 ... 0.57 ... -1 ...
dput(df1)
structure(list(x0 = c(0.702166747375488, 0.205532096598193, 0.0704982518296982,
-0.159150628995597, -0.162625494967927, -0.331660025490033, -0.099135847436449,
-0.137985446193678, -0.179304942878067, 0.0554309512268647),
x1 = c(-0.561621170364712, -0.762747775318984, 1.63791710226613,
-0.861210697757564, -1.05393723031543, 0.809872536189693,
2.85973319518198, 0.211750306033687, 1.18360826959114, -0.358159130198865
), x2 = c(-0.304711385106637, 0.365667729645747, -0.406328268107825,
-0.315315872233279, -0.477546612710489, 0.251158976293131,
-1.1263800774781, 0.229002212764429, -0.00413111289214729,
-0.252467704090853)), .Names = c("x0", "x1", "x2"), row.names = c(NA,
10L), class = "data.frame")
Upvotes: 2
Views: 418
Reputation: 59355
Here's a solution that uses foverlaps(...)
in the data.table package. Unfortunately. you need the most recent developmental version for this to work. Uses the intervals
data.frame from the other answer.
##install.packages("devtools")
# library(devtools)
# install_github("Rdatatable/data.table", build_vignettes = FALSE)
library(data.table)
y <- with(df1,data.table(row=1:nrow(df1),lo=x1, hi=x1, key=c("lo","hi")))
cuts <- foverlaps(setDT(intervals),y, by.x=c("min","max"))[,list(row,name)]
lapply(split(cuts, cuts$name),function(s)df1[sort(s$row),])
# $`[-1.05;-0.00)`
# x0 x1 x2
# 1 0.70216675 -0.5616212 -0.3047114
# 2 0.20553210 -0.7627478 0.3656677
# 4 -0.15915063 -0.8612107 -0.3153159
# 10 0.05543095 -0.3581591 -0.2524677
#
# $`[-2.05;-0.95)`
# x0 x1 x2
# 5 -0.1626255 -1.053937 -0.4775466
#
# $`[-3.05;-1.95)`
# [1] x0 x1 x2
# <0 rows> (or 0-length row.names)
#...
foverlaps(x,y,...)
does an "overlap join", that is, it finds all the records in y
which which have overlaps in x
. Overlaps are defined as values in a range between to columns in y
(say, a and b), which overlap the corresponding range in two columns in x
(say c and d). In this case we use, for x
, the intervals
data.frame (converted to a data.table), and for y
, a data.table formed with the lo and hi columns both = df$x1
.
Upvotes: 1
Reputation: 13139
I could not see a solution with creating intervals with cut that did not lead to multiple columns, so I approached it from another angle: iterate over all cutpoints and return the subset for that min and max.
intervals <- data.frame(min=c(-5,-4.05+0:3,0,0.95+0:3))
intervals$max <- rev(intervals$min)*-1
intervals$name <- with(intervals, sprintf("[%.2f;%.2f)",min,max))
res <- lapply(split(intervals,intervals$name), function(x){
return(df1[df1$x1> x$min & df1$x1 <= x$max,])
})
> head(res)
$`[-1.05;-0.00)`
x0 x1 x2
1 0.70216675 -0.5616212 -0.3047114
2 0.20553210 -0.7627478 0.3656677
4 -0.15915063 -0.8612107 -0.3153159
10 0.05543095 -0.3581591 -0.2524677
$`[-2.05;-0.95)`
x0 x1 x2
5 -0.1626255 -1.053937 -0.4775466
$`[-3.05;-1.95)`
[1] x0 x1 x2
<0 rows> (or 0-length row.names)
$`[-4.05;-2.95)`
[1] x0 x1 x2
<0 rows> (or 0-length row.names)
$`[-5.00;-3.95)`
[1] x0 x1 x2
<0 rows> (or 0-length row.names)
$`[0.00;1.05)`
x0 x1 x2
6 -0.3316600 0.8098725 0.2511590
8 -0.1379854 0.2117503 0.2290022
Upvotes: 2