如何根据另一列定义的组计算一列的排名?

发布于 2024-10-31 17:03:13 字数 696 浏览 1 评论 0原文

Windows 7 上的 R 版本 2.11.1 32 位

我得到如下数据集:

USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3

USER_A 为 1:3,USER_B 为 6:10。现在我需要输出 USER_A 和 USER_B 按 SCORE 的排名:

USER_A      ranking of USER_B
1  3  1  2  #the ranking of USER_B 6,7,10(which belong to USER_A 1)
2  2  1     #the ranking of USER_B 6,9(which belong to USER_A 2)
3  1  2     #the ranking of USER_B 8,9(which belong to USER_A 3)

事实上,我只需要输出排名:

3 1 2
2 1
1 2

很沮丧,因为每行的长度不同!我无法将它们存储在矩阵中然后输出它们。

有人能帮我解决这个问题吗?

R Version 2.11.1 32-bit on Windows 7

I get a data set as below:

USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3

the USER_A is 1:3 and the USER_B is 6:10. Now I need to output the USER_A with the ranking of USER_B by their SCORE:

USER_A      ranking of USER_B
1  3  1  2  #the ranking of USER_B 6,7,10(which belong to USER_A 1)
2  2  1     #the ranking of USER_B 6,9(which belong to USER_A 2)
3  1  2     #the ranking of USER_B 8,9(which belong to USER_A 3)

in fact, I just need to output the ranking:

3 1 2
2 1
1 2

it is upset because the length of each row is different! I could not store them in a matrix and then output them.

Could anyone help me solve this problem?

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

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

发布评论

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

评论(1

帅冕 2024-11-07 17:03:13
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

一种方法是分割数据:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

最后一行给出:

> lapply(sdf, rank)
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

一种方法是分割数据:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

最后一行给出:

1` [1] 3 1 2
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

一种方法是分割数据:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

最后一行给出:

2` [1] 2 1
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

一种方法是分割数据:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

最后一行给出:

3` [1] 1 2

另一种方法是使用aggregate(),如下所示:

aggregate(SCORE ~ USER_A, data = df, rank)

返回:

> (foo <- aggregate(SCORE ~ USER_A, data = df, rank))
  USER_A   SCORE
1      1 3, 1, 2
2      2    2, 1
3      3    1, 2

但是这里的输出有点不同,现在我们有一个数据框,第二个组件 SCORE 是一个列表,就像输出的 lapply() 版本一样:

> str(foo)
'data.frame':   3 obs. of  2 variables:
 $ USER_A: int  1 2 3
 $ SCORE :List of 3
  ..$ 0: num  3 1 2
  ..$ 1: num  2 1
  ..$ 2: num  1 2
> foo$SCORE
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

一种方法是分割数据:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

最后一行给出:

> lapply(sdf, rank)
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

一种方法是分割数据:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

最后一行给出:

1` [1] 3 1 2
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

一种方法是分割数据:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

最后一行给出:

2` [1] 2 1
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

一种方法是分割数据:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

最后一行给出:

3` [1] 1 2

另一种方法是使用aggregate(),如下所示:

aggregate(SCORE ~ USER_A, data = df, rank)

返回:

> (foo <- aggregate(SCORE ~ USER_A, data = df, rank))
  USER_A   SCORE
1      1 3, 1, 2
2      2    2, 1
3      3    1, 2

但是这里的输出有点不同,现在我们有一个数据框,第二个组件 SCORE 是一个列表,就像输出的 lapply() 版本一样:

0` [1] 3 1 2
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

一种方法是分割数据:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

最后一行给出:

> lapply(sdf, rank)
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

一种方法是分割数据:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

最后一行给出:

1` [1] 3 1 2
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

一种方法是分割数据:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

最后一行给出:

2` [1] 2 1
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

一种方法是分割数据:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

最后一行给出:

3` [1] 1 2

另一种方法是使用aggregate(),如下所示:

aggregate(SCORE ~ USER_A, data = df, rank)

返回:

> (foo <- aggregate(SCORE ~ USER_A, data = df, rank))
  USER_A   SCORE
1      1 3, 1, 2
2      2    2, 1
3      3    1, 2

但是这里的输出有点不同,现在我们有一个数据框,第二个组件 SCORE 是一个列表,就像输出的 lapply() 版本一样:

1` [1] 2 1
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

一种方法是分割数据:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

最后一行给出:

> lapply(sdf, rank)
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

一种方法是分割数据:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

最后一行给出:

1` [1] 3 1 2
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

一种方法是分割数据:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

最后一行给出:

2` [1] 2 1
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

一种方法是分割数据:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

最后一行给出:

3` [1] 1 2

另一种方法是使用aggregate(),如下所示:

aggregate(SCORE ~ USER_A, data = df, rank)

返回:

> (foo <- aggregate(SCORE ~ USER_A, data = df, rank))
  USER_A   SCORE
1      1 3, 1, 2
2      2    2, 1
3      3    1, 2

但是这里的输出有点不同,现在我们有一个数据框,第二个组件 SCORE 是一个列表,就像输出的 lapply() 版本一样:

2` [1] 1 2
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

One way is to split the data:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

The last line gives:

> lapply(sdf, rank)
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

One way is to split the data:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

The last line gives:

1` [1] 3 1 2
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

One way is to split the data:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

The last line gives:

2` [1] 2 1
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

One way is to split the data:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

The last line gives:

3` [1] 1 2

An alternative is to use aggregate() as in:

aggregate(SCORE ~ USER_A, data = df, rank)

Which returns:

> (foo <- aggregate(SCORE ~ USER_A, data = df, rank))
  USER_A   SCORE
1      1 3, 1, 2
2      2    2, 1
3      3    1, 2

But the output is a bit different here, now we have a data frame, with the second component SCORE being a list, just like the lapply() version outputted:

> str(foo)
'data.frame':   3 obs. of  2 variables:
 $ USER_A: int  1 2 3
 $ SCORE :List of 3
  ..$ 0: num  3 1 2
  ..$ 1: num  2 1
  ..$ 2: num  1 2
> foo$SCORE
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

One way is to split the data:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

The last line gives:

> lapply(sdf, rank)
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

One way is to split the data:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

The last line gives:

1` [1] 3 1 2
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

One way is to split the data:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

The last line gives:

2` [1] 2 1
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

One way is to split the data:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

The last line gives:

3` [1] 1 2

An alternative is to use aggregate() as in:

aggregate(SCORE ~ USER_A, data = df, rank)

Which returns:

> (foo <- aggregate(SCORE ~ USER_A, data = df, rank))
  USER_A   SCORE
1      1 3, 1, 2
2      2    2, 1
3      3    1, 2

But the output is a bit different here, now we have a data frame, with the second component SCORE being a list, just like the lapply() version outputted:

0` [1] 3 1 2
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

One way is to split the data:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

The last line gives:

> lapply(sdf, rank)
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

One way is to split the data:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

The last line gives:

1` [1] 3 1 2
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

One way is to split the data:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

The last line gives:

2` [1] 2 1
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

One way is to split the data:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

The last line gives:

3` [1] 1 2

An alternative is to use aggregate() as in:

aggregate(SCORE ~ USER_A, data = df, rank)

Which returns:

> (foo <- aggregate(SCORE ~ USER_A, data = df, rank))
  USER_A   SCORE
1      1 3, 1, 2
2      2    2, 1
3      3    1, 2

But the output is a bit different here, now we have a data frame, with the second component SCORE being a list, just like the lapply() version outputted:

1` [1] 2 1
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

One way is to split the data:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

The last line gives:

> lapply(sdf, rank)
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

One way is to split the data:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

The last line gives:

1` [1] 3 1 2
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

One way is to split the data:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

The last line gives:

2` [1] 2 1
df <- read.table(con <- textConnection("USER_A USER_B SCORE
1        6      0.2
1        7      0.1
1        10     0.15
2        6      0.2
2        9      0.12
3        8      0.15
3        9      0.3
"), header = TRUE)
close(con)

One way is to split the data:

sdf <- with(df, split(SCORE, f = USER_A))
lapply(sdf, rank)

The last line gives:

3` [1] 1 2

An alternative is to use aggregate() as in:

aggregate(SCORE ~ USER_A, data = df, rank)

Which returns:

> (foo <- aggregate(SCORE ~ USER_A, data = df, rank))
  USER_A   SCORE
1      1 3, 1, 2
2      2    2, 1
3      3    1, 2

But the output is a bit different here, now we have a data frame, with the second component SCORE being a list, just like the lapply() version outputted:

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