重命名r中的列数据

时间:2018-11-06 22:04:48

标签: r dplyr tidy

我想重命名一列观察结果。 **Code to Reproduce** library(cansim) library(dplyr) HPI_LIST <- c("v111955442", "v111955445", "v111955448", "v111955451", "v111955454", "v111955457", "v111955460", "v111955463", "v111955466", "v111955469", "v111955472", "v111955475", "v111955478", "v111955481", "v111955484", "v111955487", "v111955490", "v111955493", "v111955496", "v111955499", "v111955502", "v111955505", "v111955508", "v111955511", "v111955514", "v111955517", "v111955520", "v111955523", "v111955526", "v111955529", "v111955532", "v111955535", "v111955538", "v111955541", "v111955544", "v111955547", "v111955550", "v111955553", "v111955556", "v111955559") NewHPIData <- get_cansim("18-10-0205-01") NewHPI <- NewHPIData %>% filter(VECTOR %in% HPI_LIST, REF_DATE > 2018) %>% arrange((COORDINATE)) %>% select(REF_DATE, GEO, VALUE) %>% mutate(VALUE = VALUE / 100) %>% arrange(GEO) 如下所示。 GEO列有40个不同的名称,我想重命名。我创建了另一个列表,其中包含40个我要使用的替代名称。尽管我有成千上万的观察结果,但我只有40个不同的名字。非常感谢您的帮助!

DataFrame

void DrawRect(v2f MinPoint, v2f MaxPoint) 
    {
        glBegin(GL_QUADS);

        glColor3f(1.0f, 0.0f, 0.0f); //Red color
        glVertex2f(MaxPoint.x, MaxPoint.y);
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex2f(MinPoint.x, MaxPoint.y);
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex2f(MinPoint.x, MinPoint.y);
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex2f(MaxPoint.x, MinPoint.y);

        glEnd();
        glFlush();
    }

我想在GEO中更新名称,例如,我想用“新房屋价格指数-AB(x 100)”重命名艾伯塔省

1 个答案:

答案 0 :(得分:1)

将您的姓名地图定义为列表

nameMap <- list( "Alberta" = "New Housing Price Index - AB (x 100)",
                "Atlantic Region" = "New Name 1",
                "British Columbia" = "New Name 2"
                ## and so on )

然后使用recodeGEO列上调用mutate_at

NewHPI %>% mutate_at( "GEO", recode, !!!nameMap )
# # A tibble: 320 x 3
#    REF_DATE GEO                                  VALUE
#    <chr>    <chr>                                <dbl>
#  1 2018-01  New Housing Price Index - AB (x 100) 1.00 
#  2 2018-02  New Housing Price Index - AB (x 100) 0.998
#  3 2018-03  New Housing Price Index - AB (x 100) 0.996
#  4 2018-04  New Housing Price Index - AB (x 100) 0.996
#  5 2018-05  New Housing Price Index - AB (x 100) 0.996
#  6 2018-06  New Housing Price Index - AB (x 100) 0.998
#  7 2018-07  New Housing Price Index - AB (x 100) 0.999
#  8 2018-08  New Housing Price Index - AB (x 100) 0.994
#  9 2018-01  New Name 1                           1.00 
# 10 2018-02  New Name 1                           1.00 

需要!!!运算符,因为recode使用...接受任意数量的参数,但是您有一个预定义的列表。这种机制称为unquote-splicing