R 将数据帧中定义的间隔映射到向量

R 将数据帧中定义的间隔映射到向量,r,intervals,R,Intervals,我有一个具有间隔定义的数据帧: ints <- read.table(header=T, sep=";", stringsAsFactors = FALSE, na.strings = 'NA', text=" minValue;minOperato;maxValue;maxOperator;class 3914;>=;NA;NA;[3914,Inf) NA;NA;1373;<;[ -Inf,1373) 1373;>=;1806;<;[1373,1806) 2777

我有一个具有间隔定义的数据帧:

ints <- read.table(header=T, sep=";", stringsAsFactors = FALSE, na.strings = 'NA', text="
minValue;minOperato;maxValue;maxOperator;class
3914;>=;NA;NA;[3914,Inf)
NA;NA;1373;<;[ -Inf,1373)
1373;>=;1806;<;[1373,1806)
2777;>=;3914;<;[2777,3914)
1806;>=;2777;<;[1806,2777)
")
现在,我想得到每个间隔的
sd
数据帧中的0和1的数量,并将结果合并到
ints
数据帧中

我想我会使用
cut

breaks <- c(-Inf, ints$minValue[order(ints$minValue)], Inf)
breaks <- breaks[!is.na(breaks)]
out <- as.data.frame.matrix(table(cut(sd$x, breaks, right = FALSE), sd$y))

中断一种解决方案是用一个简单的id(序列)替换间隔。这应该针对ints和out data.frames进行。每个id标识一个间隔。一旦这样做,合并就很简单了

## first I extract the intevals from ints in ordered manner
id <- !is.na(ints$minValue)&!is.na(ints$maxValue)
class_factor <- 
  c(ints$class[which(is.na(ints$minValue))],
     ints$class[id][order(ints$minValue[id])],
     ints$class[which(is.na(ints$maxValue))])

## add an id column that identify each interval in ints data.frame
ints <- merge(data.frame(class=class_factor,id = seq_along(class_factor)),ints)

##  Do same thing in out uisng lables=FALSE  as a cut argument
out <- as.data.frame.matrix(table(cut(sd$x, breaks, right = FALSE,
         labels=FALSE), sd$y))  ## here the trick 

## merge ints and out 
merge(out,ints,by.x=0,by.y="id")

#     Row.names   0   1        class minValue minOperato maxValue maxOperator
#   1         1 132 146 [ -Inf,1373)       NA       <NA>     1373           <
#   2         2  45  38  [1373,1806)     1373         >=     1806           <
#   3         3  98  99  [1806,2777)     1806         >=     2777           <
#   4         4  98 110  [2777,3914)     2777         >=     3914           <
#   5         5 125 109   [3914,Inf)     3914         >=       NA        <NA>
##首先,我以有序的方式从int中提取intevals
id=NA

您可能希望添加参数
dig.lab=4
以剪切到小数点。@lukeA Cool,这对这种情况有效,但不是通用的,对吗?嗯,您可以将其提高到
50
nchar(最大值(未列出(ints[,c(1,3)]),na.rm TRUE))
,或者你是什么意思?我在
ints
中没有受控制的数字-可能是大整数,也可能是小数位数不同的极小数字。我认为如果我将
dig.lab
设置为50,if将适应所有情况,或者它可以在某些情况下中断。
## first I extract the intevals from ints in ordered manner
id <- !is.na(ints$minValue)&!is.na(ints$maxValue)
class_factor <- 
  c(ints$class[which(is.na(ints$minValue))],
     ints$class[id][order(ints$minValue[id])],
     ints$class[which(is.na(ints$maxValue))])

## add an id column that identify each interval in ints data.frame
ints <- merge(data.frame(class=class_factor,id = seq_along(class_factor)),ints)

##  Do same thing in out uisng lables=FALSE  as a cut argument
out <- as.data.frame.matrix(table(cut(sd$x, breaks, right = FALSE,
         labels=FALSE), sd$y))  ## here the trick 

## merge ints and out 
merge(out,ints,by.x=0,by.y="id")

#     Row.names   0   1        class minValue minOperato maxValue maxOperator
#   1         1 132 146 [ -Inf,1373)       NA       <NA>     1373           <
#   2         2  45  38  [1373,1806)     1373         >=     1806           <
#   3         3  98  99  [1806,2777)     1806         >=     2777           <
#   4         4  98 110  [2777,3914)     2777         >=     3914           <
#   5         5 125 109   [3914,Inf)     3914         >=       NA        <NA>