类似开关的问卷评分功能

发布于 2024-12-03 07:42:14 字数 362 浏览 0 评论 0原文

我最近认真完成了 PHP/JS 编码,我有点失去了 R 能力。虽然这个问题可以在 PHP/JS 中轻松解决,但解决这个问题最有效的方法是什么:我必须对调查问卷进行评分,并且我有以下情况:

raw    t
5      0
6      2
7-9    3
10-12  4
15-20  5

如果 x 等于或是在 raw 给定的范围内,应返回 t 中相应行中的值。当然,这可以通过 for 循环或 switch 来完成,但想象一下 raw 中非常长的值范围集。你会如何解决这个问题?

I'd done a serious PHP/JS coding recently, and I kind-of lost my R muscle. While this problem can be easily tackled within PHP/JS, what is the most efficient way of solving this one: I have to grade a questionnaire, and I have following scenario:

raw    t
5      0
6      2
7-9    3
10-12  4
15-20  5

if x equals to, or is within range given in raw, value in according row in t should be returned. Of course, this can be done with for loop, or switch, but just imagine very lengthy set of value ranges in raw. How would you tackle this one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蒲公英的约定 2024-12-10 07:42:14

我们似乎遗漏了示例的一部分,因为没有提到“x”

dat <- read.table(textConnection("raw    t
 5      0
 6      2
 7-9    3
 10-12  4
 15-20  5"), header=TRUE, stringsAsFactors=FALSE)
dat$bot <- as.numeric( sapply( sapply(dat$raw, strsplit, "-"), "[", 1 ))
get.t <- function(x) findInterval(x, dat$bot)
 get.t(8)
#[1] 3
> dat$t[get.t(6)]
[1] 2
> dat$t[get.t(5)]
[1] 0

We seem to be missing a part of the example because there in no mention of "x"

dat <- read.table(textConnection("raw    t
 5      0
 6      2
 7-9    3
 10-12  4
 15-20  5"), header=TRUE, stringsAsFactors=FALSE)
dat$bot <- as.numeric( sapply( sapply(dat$raw, strsplit, "-"), "[", 1 ))
get.t <- function(x) findInterval(x, dat$bot)
 get.t(8)
#[1] 3
> dat$t[get.t(6)]
[1] 2
> dat$t[get.t(5)]
[1] 0
于我来说 2024-12-10 07:42:14

我会简单地使用类似于 Corbin 提到的索引方案,但由于他没有提供示例,这里有一个简单的示例:

m <- cbind(c(5:12,15:20),
           rep(c(0,2,3,4,5),times = c(1,1,3,3,6)))

m[m[,1] == 11,2]
[1] 4

I would simply use an indexing scheme kind of like what Corbin alluded to, but since he didn't provide an example, here's a simple one:

m <- cbind(c(5:12,15:20),
           rep(c(0,2,3,4,5),times = c(1,1,3,3,6)))

m[m[,1] == 11,2]
[1] 4
安人多梦 2024-12-10 07:42:14

注意:与西蒙娜的回答非常相似,因为我开始输入这个内容。不过最后有一个注释。我给出的索引方法本质上是西蒙娜的答案。

某处一定会涉及到一个循环。

我要做的伪代码是这样的:

score = blah

for each raw => t
    break raw into rMin -> rMax
    if(rMin <= score and rMax >= score)
        return t

它避免了必须循环 rMin 和 rMax 之间的每个数字(这就是我假设你的意思),但如果没有某种索引,那就是最好的将会得到。

注意:如果您对此有大量调用,并且索引实际上值得您花时间,那么最简单的索引类型就是分数的哈希映射 -> 。 t 条目。

基本上,您会将示例数据解析为以下内容:

index[5] = 0
index[6] = 2
index[7] = 3
index[8] = 3
index[9] = 3

您需要仔细权衡构建索引是否比仅循环范围更耗时。

注:索引方式其实就是Simone所说的。

Note: very similar to Simone's answer as I started typing this a bit back. Has a note at the end though. The indexing approach I give is essentially Simone's answer.

There will have to be a loop involved somewhere.

The pseudo code of what I would do is something like:

score = blah

for each raw => t
    break raw into rMin -> rMax
    if(rMin <= score and rMax >= score)
        return t

It avoids having to loop over each number between rMin and rMax (which is what I'm assuming you meant), but without some kind of indexing, that is the best you're going to get.

Note: if you have a ton of calls to this, and indexing would actually be worth your while, the easiest type of indexing would just be a hash map of score -> t entries.

Basically you would parse your example data into something like:

index[5] = 0
index[6] = 2
index[7] = 3
index[8] = 3
index[9] = 3

You would need to carefully weigh if building the index would be more time consuming than just looping over the ranges.

Note: the indexing approach is actually what Simone said.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文