如何在R中一次构建多列

发布于 2025-01-18 08:02:27 字数 872 浏览 0 评论 0原文

以下是示例数据。我知道如何为每个时间段构建排名列,但这不是任务。我有一个更大的数据集,其中包含 2001 年到 2022 年的每月数据,但希望避免手动执行此操作。有没有办法为一系列列构建排名列。在本例中,它将是 3 个新列。每个人都会将值从最大到最小排列。

 area <- c("Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware")
 sept2020 <- c(.120,.125,.130,.110,.095,.045,.131,.029)
 oct2020 <- c(.121,.129,.128,.119,.099,.041,.138,.028)
 nov2020 <- c(.119,.128,.129,.118,.091,.048,.139,.037)

 percent <- data.frame(area,sept2020,oct2020,nov2020)

所需的结果将按原样显示,但还有两个排名列.. 对于 oct2020 和 nov2020

  area         sept2020    rank1
 Alabama           .120       4
 Alaska            .125       3
 Arizona           .130       2
 Arkansas          .110       5
 California        .095       6
 Colorado          .045       7
 Connecticut       .131       1
 Delaware          .029       8

 

Below is the sample data. I know how to construct a rank column for each time period but that is not the task. I have a larger data set that has monthly data from 2001 to 2022 but looking to avoid doing this manually. Is there a way to construct a rank column for a range of columns. In this case, it would would be 3 new columns. Each one would rank the values from largest to smallest.

 area <- c("Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware")
 sept2020 <- c(.120,.125,.130,.110,.095,.045,.131,.029)
 oct2020 <- c(.121,.129,.128,.119,.099,.041,.138,.028)
 nov2020 <- c(.119,.128,.129,.118,.091,.048,.139,.037)

 percent <- data.frame(area,sept2020,oct2020,nov2020)

The desired result would appear as such but with two more rank columns.. for oct2020 and nov2020

  area         sept2020    rank1
 Alabama           .120       4
 Alaska            .125       3
 Arizona           .130       2
 Arkansas          .110       5
 California        .095       6
 Colorado          .045       7
 Connecticut       .131       1
 Delaware          .029       8

 

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

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

发布评论

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

评论(2

沉鱼一梦 2025-01-25 08:02:27

1)dplyr 在上使用 :

library(dplyr)

percent %>%
  mutate(across(-1, ~ rank(desc(.)), .names = "{.col}_rank"))

gives:

         area sept2020 oct2020 nov2020 sept2020_rank oct2020_rank nov2020_rank
1     Alabama    0.120   0.121   0.119             4            4            4
2      Alaska    0.125   0.129   0.128             3            2            3
3     Arizona    0.130   0.128   0.129             2            3            2
4    Arkansas    0.110   0.119   0.118             5            5            5
5  California    0.095   0.099   0.091             6            6            6
6    Colorado    0.045   0.041   0.048             7            7            7
7 Connecticut    0.131   0.138   0.139             1            1            1
8    Delaware    0.029   0.028   0.037             8            8            8

2)基础r 基本r解决方案将是以下内容。它提供了相似的输出。

Rank <- function(nm, x) rank(-x)
cbind(percent, mapply(Rank, paste0(names(percent)[-1], "_rank"), percent[-1]))

1) dplyr Use across like this:

library(dplyr)

percent %>%
  mutate(across(-1, ~ rank(desc(.)), .names = "{.col}_rank"))

giving:

         area sept2020 oct2020 nov2020 sept2020_rank oct2020_rank nov2020_rank
1     Alabama    0.120   0.121   0.119             4            4            4
2      Alaska    0.125   0.129   0.128             3            2            3
3     Arizona    0.130   0.128   0.129             2            3            2
4    Arkansas    0.110   0.119   0.118             5            5            5
5  California    0.095   0.099   0.091             6            6            6
6    Colorado    0.045   0.041   0.048             7            7            7
7 Connecticut    0.131   0.138   0.139             1            1            1
8    Delaware    0.029   0.028   0.037             8            8            8

2) Base R A base R solution would be the following. It gives similar output.

Rank <- function(nm, x) rank(-x)
cbind(percent, mapply(Rank, paste0(names(percent)[-1], "_rank"), percent[-1]))
别理我 2025-01-25 08:02:27

听起来您可能正在从dplyr中寻找dense_rank函数:

percent %>%
        mutate(rank1 = dense_rank(desc(sept2020))

然后,您可以使用dense_rank中的Oct2020和Nov20重复该代码,以创建下两个排名变量。

It sounds like you might be looking for the dense_rank function from dplyr:

percent %>%
        mutate(rank1 = dense_rank(desc(sept2020))

And then you could simply repeat that code, using oct2020 and nov2020 in the dense_rank, to create the next two ranking variables.

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