R语言 在每个列向量旁边添加秩列

fruv7luv  于 2023-03-05  发布在  其他
关注(0)|答案(1)|浏览(137)

我有一个数据框,其中的列表示年度价格回报,索引包含位置。我想在每个年度列旁边添加新列,其中包含该行值在该行所在列中的排名。下面的脚本只创建一个新数据框,不保留初始列及其值:

test <- yearRetsMSA2 %>%
        mutate(across(c(cnam_year2[1]:cnam_year2[length(cnam_year2)]), rank))

以下是我的数据样本:

structure(list(`1995 Return` = c(0.0151000000000001, 0.0463), 
        `1996 Return` = c(0.0361540734902965, 0.050750262830928), 
        `1997 Return` = c(0.036223616657159, 0.049208659268692), 
        `1998 Return` = c(0.0213781080833104, 0.0508019072388384), 
        `1999 Return` = c(0.0369205892921309, 0.023265407144625), 
        `2000 Return` = c(0.0177596811920644, 0.042892848504394), 
        `2001 Return` = c(0.0474123255022132, 0.0538074990336297), 
        `2002 Return` = c(0.0282811865095489, 0.0258968527620864), 
        `2003 Return` = c(-0.00505808899075322, 0.0240989702517163
        ), `2004 Return` = c(0.0660100087377868, 0.0309335940227635
        ), `2005 Return` = c(0.0777943368107303, 0.0308859387699811
        ), `2006 Return` = c(0.0893252212389382, -0.00683311432325884
        ), `2007 Return` = c(0.0338283828382837, -0.0302990209050013
        ), `2008 Return` = c(0.0355454601264658, -0.0375221721926593
        ), `2009 Return` = c(0.00361631491581682, -0.0233909838389567
        ), `2010 Return` = c(0.000472561876070809, -0.0121933517201336
        ), `2011 Return` = c(-0.0144653716714885, -0.0449669360764144
        ), `2012 Return` = c(0.0181524083393243, -0.012925065394676
        ), `2013 Return` = c(0.0614886731391586, 0.0127825409197193
        ), `2014 Return` = c(0.0437361419068736, 0.0333230721871633
        ), `2015 Return` = c(0.0364331616124065, 0.0430475906755046
        ), `2016 Return` = c(0.0472457084294133, 0.0165655123170296
        ), `2017 Return` = c(0.0218231638694526, 0.0523986794970852
        ), `2018 Return` = c(0.0755159699276924, 0.036975238603751
        ), `2019 Return` = c(0.0231967943009797, 0.0610800025744997
        ), `2020 Return` = c(0.0486488838605805, 0.0724857454810142
        ), `2021 Return` = c(0.196107722312129, 0.140093886092416
        ), `2022 Return` = c(0.069071986123157, 0.119059430499058
        )), row.names = c("Abilene, TX", "Akron, OH"), class = "data.frame")
68bkxrlz

68bkxrlz1#

base R中,我们可以通过循环感兴趣的列来分配_rank列,并得到rank

rnk_colnms <- paste0(cnam_year2, "_rank")
yearRetsMSA2[rnk_colnms] <- lapply(yearRetsMSA2[cnam_year2], rank)

或使用dplyr

library(dplyr)
cnam_year2 <- colnames(yearRetsMSA2)
yearRetsMSA2 %>%
   rownames_to_column('rn') %>% 
  mutate(across(all_of(cnam_year2), rank, .names = "{.col}_rank")) %>% 
  select(rn,  order(readr::parse_number(names(.)[-1])) + 1)
  • 输出
rn 1995 Return 1995 Return_rank 1996 Return 1996 Return_rank 1997 Return 1997 Return_rank 1998 Return 1998 Return_rank
1 Abilene, TX      0.0151                1  0.03615407                1  0.03622362                1  0.02137811                1
2   Akron, OH      0.0463                2  0.05075026                2  0.04920866                2  0.05080191                2
  1999 Return 1999 Return_rank 2000 Return 2000 Return_rank 2001 Return 2001 Return_rank 2002 Return 2002 Return_rank  2003 Return
1  0.03692059                2  0.01775968                1  0.04741233                1  0.02828119                2 -0.005058089
2  0.02326541                1  0.04289285                2  0.05380750                2  0.02589685                1  0.024098970
  2003 Return_rank 2004 Return 2004 Return_rank 2005 Return 2005 Return_rank  2006 Return 2006 Return_rank 2007 Return 2007 Return_rank
1                1  0.06601001                2  0.07779434                2  0.089325221                2  0.03382838                2
2                2  0.03093359                1  0.03088594                1 -0.006833114                1 -0.03029902                1
  2008 Return 2008 Return_rank  2009 Return 2009 Return_rank   2010 Return 2010 Return_rank 2011 Return 2011 Return_rank 2012 Return
1  0.03554546                2  0.003616315                2  0.0004725619                2 -0.01446537                2  0.01815241
2 -0.03752217                1 -0.023390984                1 -0.0121933517                1 -0.04496694                1 -0.01292507
  2012 Return_rank 2013 Return 2013 Return_rank 2014 Return 2014 Return_rank 2015 Return 2015 Return_rank 2016 Return 2016 Return_rank
1                2  0.06148867                2  0.04373614                2  0.03643316                1  0.04724571                2
2                1  0.01278254                1  0.03332307                1  0.04304759                2  0.01656551                1
  2017 Return 2017 Return_rank 2018 Return 2018 Return_rank 2019 Return 2019 Return_rank 2020 Return 2020 Return_rank 2021 Return
1  0.02182316                1  0.07551597                2  0.02319679                1  0.04864888                1   0.1961077
2  0.05239868                2  0.03697524                1  0.06108000                2  0.07248575                2   0.1400939
  2021 Return_rank 2022 Return 2022 Return_rank
1                2  0.06907199                1
2                1  0.11905943                2

相关问题