2

编辑:我在之前共享的代码中犯了一个错误。我用“b”替换了“垃圾箱”,但错过了一个......

我现在也使用正确的data.frame(y而不是原来的df.score)

新代码:

# some data
x <- runif(1000)
x2 <- rnorm(1000)
y <- data.frame(x,x2)
# we want to bin the dataframe y acording to values in x into b bins
b = 10
bins=10

# we create breaks in several ways
breaks=unique(quantile(x, probs=seq.int(0,1, by=1/b)))
breaks=unique(quantile(y$x, probs=seq.int(0,1, length.out=b+1)))

# now to the question
# this wokrs
y$b <- with(y, cut(x, breaks=unique(quantile(x, probs=seq.int(0,1, length.out=11))), include.lowest=TRUE))
table(y$b)
# this works too
y$b2 <- with(y, cut(x, breaks=unique(quantile(x, probs=seq.int(0,1, length.out=(bins+1)))), include.lowest=TRUE))
table(y$b2)
# this does not work
y$b3 <- with(y, cut(x, breaks=unique(quantile(x, probs=seq.int(0,1, length.out=(b+1)))), include.lowest=TRUE))

seq.int(0, 1, length.out = (b + 1)) 中的错误:'length.out' 必须是非负数另外:警告消息:在 Ops.factor(b, 1) 中:+对因子没有意义

现在,如果我将代码拆分,则没有问题!!!

brks=unique(quantile(x, probs=seq.int(0,1, length.out=(b + 1))))
y$b3 <- with(y, cut(x, breaks=brks, include.lowest=TRUE))

我在这里迷路了……

这是更多动态代码的一部分,根据数据集中的细节编织在一起。

所以我想动态创建垃圾箱并报告它们。该代码现在可以工作,但我不明白为什么当我使用“bins”这个词时,代码可以工作,而当使用“b”时它会失败......?


OLD 从这里开始,我需要将 bin 动态添加到数据框中,以便稍后报告它们。

# some data
x <- runif(1000)
x2 <- rnorm(1000)
y <- data.frame(x,x2)
# we want to bin the dataframe y acording to values in x into b bins
b = 10

# we create breaks in several ways
breaks=unique(quantile(x, probs=seq.int(0,1, by=1/b)))
breaks=unique(quantile(y$x, probs=seq.int(0,1, length.out=b+1)))

# now to question
# this works

y$bins <- with(df.score, cut(x, breaks=unique(quantile(Pchurn, probs=seq.int(0,1, length.out=11))), include.lowest=TRUE))
table(y$bins)

因此,如果我想直接使用 bin var 做同样的事情,它会失败:

# this does not work
y$bins <- with(df.score, cut(x, breaks=unique(quantile(Pchurn, probs=seq.int(0,1, length.out=bins+1))), include.lowest=TRUE))


Error in seq.int(0, 1, length.out = (bins + 1)) : 
  'length.out' must be a non-negative number
In addition: Warning message:
In Ops.factor(bins, 1) : + not meaningful for factors

我在这里想念什么?

4

1 回答 1

2

我认为您想要这个(在“#this 不起作用”下方的长度参数 calc 中替换bbins

y$bins <- with(df.score, cut(x, 
                    breaks=unique(quantile(Pchurn, 
                                         probs=seq.int(0,1, length.out=b+1))), 
                    include.lowest=TRUE))

如果没有分数变量和更完整的目标描述,很难进行测试,但至少代码不会在工作区中引发错误。

 df.score=data.frame(Pchurn=rnorm(100), x=rnorm(100))
于 2013-09-27T17:31:29.000 回答