如何在R中一次构建多列
以下是示例数据。我知道如何为每个时间段构建排名列,但这不是任务。我有一个更大的数据集,其中包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1)dplyr 在上使用 :
gives:
2)基础r 基本r解决方案将是以下内容。它提供了相似的输出。
1) dplyr Use
across
like this:giving:
2) Base R A base R solution would be the following. It gives similar output.
听起来您可能正在从
dplyr
中寻找dense_rank函数:然后,您可以使用dense_rank中的Oct2020和Nov20重复该代码,以创建下两个排名变量。
It sounds like you might be looking for the dense_rank function from
dplyr
:And then you could simply repeat that code, using oct2020 and nov2020 in the dense_rank, to create the next two ranking variables.