使用多个条件重命名r

时间:2019-04-12 21:48:58

标签: r function dataframe data.table tidyverse

鉴于下面的数据框架包含三个变量,每个变量重复使用完全相同的名称重复三次,我想要一个函数,该函数根据名称和位置将具有相同名称的每组变量的每个元素重命名为示例第一次添加.m,每个变量集中具有相同名称的每个变量,第二次.l,第三次添加.u

我知道我可以使用thisthisthisother解决方案来manually做我想做的事情。但就我而言,我的某些变量具有相同的名称,因此对我来说唯一显而易见的选择是使用列位置来标识和重命名变量,这对于大型数据集是不可行的。

#data

dt<-structure(list(general = c(0.348886829202466, 
0.203589313542743, 0.90655704224777, 0.344599044114144, 0.301264059075478
), vocation = c(0.90007128724776, 0.25093350439507, 
0.840003175556854, 0.0763147760239838, 0.765717248505423), general = c(8.48536430252404, 
0.61215079555521, 0.33908999482637, 0.959941654100972, 0.577845916408486, 
0.551543184240344), vocation = c(208.71134987404, 1.68880630570487, 
0.428696219910234, 0.890082828242642, 0.16628759312575, 1.49119733964464
), general = c(239.145164960882, 1.07406919703872, 0.564774361632725, 
1.01646993662232, 0.968969324823085, 1.00974502240828), vocation = c(5980.31502662087, 
3.16871205491912, 0.732387049741986, 0.943148150133152, 0.362335645443876, 
2.90403475970219)), class = "data.frame", row.names = c( 
"sesmiddle", "seshigh", "write", "schtypprivate", "honorsenrolled"
))

#期望的输出

        general.m  general.l   general.u   vocation.m  vocation.l  vocation.u    
sesmiddle 0.3488868 0.6121508   1.0740692  0.90007129  1.6888063   3.1687121   
seshigh   0.2035893 0.3390900   0.5647744  0.25093350  0.4286962   0.7323870
write     0.9065570 0.9599417   1.0164699  0.84000318  0.8900828   0.9431482
schtypprivate  0.3445990 0.5778459   0.9689693 0.07631478   0.1662876    0.3623356
honorsenrolled 0.3012641 0.5515432   1.0097450 0.76571725   1.4911973    2.9040348

在此先感谢您的帮助。如果有任何不清楚的地方,请在评论中让我知道。

1 个答案:

答案 0 :(得分:1)

make.unique可能是一般的使用方式,但它总是会附加数字。这是适合您情况的解决方案:

f <- function(x){

  nms <- names(x)

  num <- sapply(1:length(nms), function(i) sum(nms[i] == nms[1:i]))

  suffix <- ifelse(num == 1, ".m", ifelse(num == 2, ".l", ".u"))

  names(x) <- paste0(nms, suffix)

  x

}

f(dt)
#               general.m vocation.m general.l  vocation.l   general.u   vocation.u
#sesmiddle      0.3488868 0.90007129 8.4853643 208.7113499 239.1451650 5980.3150266
#seshigh        0.2035893 0.25093350 0.6121508   1.6888063   1.0740692    3.1687121
#write          0.9065570 0.84000318 0.3390900   0.4286962   0.5647744    0.7323870
#schtypprivate  0.3445990 0.07631478 0.9599417   0.8900828   1.0164699    0.9431482
#honorsenrolled 0.3012641 0.76571725 0.5778459   0.1662876   0.9689693    0.3623356