如何重命名整数以基于键

时间:2018-11-06 19:16:06

标签: r dplyr recode

我需要根据列Quadrat的值创建一个新的因子列。有9个正交词,称为Sponge的新列将类似于:

如果Quadrat = 1,4,9,则为“旧增长”
如果Quadrat = 3,6,7
,则为“缺席” 如果Quadrat = 2,5,8

,则为“新增长”

很抱歉,如果答案很简单,我做了检查:How to convert integer to factor in R? 而且我也在尝试使用recode_factor。这是我的代码:

library(dplyr)
key <- list(`1,4,9` = "Old Growth", `3,6,7` = "Absent", `2,5,8` = "New Growth")
df <- mutate(df, Sponge = recode_factor(Quadrat, key))

我收到错误消息:

Error in mutate_impl(.data, dots) : 
  Evaluation error: Vector 1 must be length 108 or one, not 3.

如果重要的话,实际数据比我在此处包含的数据集具有更多的条目。谢谢您的帮助。

df <- structure(list(Quadrat = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 
4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 
9L, 9L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 
5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 1L, 1L, 1L, 
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 
7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 
3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 
9L, 9L, 9L), Month = structure(c(4L, 4L, 4L, 3L, 3L, 3L, 7L, 
7L, 7L, 1L, 1L, 1L, 8L, 8L, 8L, 6L, 6L, 6L, 5L, 5L, 5L, 2L, 2L, 
2L, 9L, 9L, 9L, 4L, 4L, 4L, 3L, 3L, 3L, 7L, 7L, 7L, 1L, 1L, 1L, 
8L, 8L, 8L, 6L, 6L, 6L, 5L, 5L, 5L, 2L, 2L, 2L, 9L, 9L, 9L, 4L, 
4L, 4L, 3L, 3L, 3L, 7L, 7L, 7L, 1L, 1L, 1L, 8L, 8L, 8L, 6L, 6L, 
6L, 5L, 5L, 5L, 2L, 2L, 2L, 9L, 9L, 9L, 4L, 4L, 4L, 3L, 3L, 3L, 
7L, 7L, 7L, 1L, 1L, 1L, 8L, 8L, 8L, 6L, 6L, 6L, 5L, 5L, 5L, 2L, 
2L, 2L, 9L, 9L, 9L), .Label = c("Apr", "Aug", "Feb", "Jan", "Jul", 
"Jun", "Mar", "May", "Sep"), class = "factor"), PopDens = c(65.6011820777785, 
18.4913752602879, 12.151802276494, 68.0740840677172, 50.9832500135526, 
36.8684287818614, 52.0825074084569, 26.8776902493555, 49.2173263626173, 
25.5460870559327, 5.4171769618988, 34.4303709487431, 44.3439512783661, 
2.25230997451581, 61.2502326716203, 25.9035727053415, 32.339118222706, 
24.1017888628412, 12.340617884649, 53.3521768709179, 26.0048255382571, 
52.8581868957262, 31.9503199581522, 18.1601244299673, 34.228305231547, 
2.09199664392509, 22.6402857622597, 4.48008164577186, 48.2082461479586, 
65.4937081446406, 5.43837511213496, 32.8203339113388, 4.44421968702227, 
19.8568186087068, 24.2561273102183, 12.3652934685815, 39.0541164302267, 
16.1970243314281, 12.9826903613284, 36.3537323835772, 48.7148000504822, 
11.5067498446442, 68.7493303583469, 60.7505214684643, 49.3874175737146, 
63.0705459746532, 23.721419940237, 53.4379795142449, 57.7867246468086, 
38.4747762591578, 8.43540686019696, 20.5636212413665, 28.7687741059344, 
53.2144687068649, 32.0859562589321, 10.5120962983929, 53.4312571119517, 
13.6547974413261, 31.3038802060764, 14.5005466006696, 6.03453303268179, 
62.6867637028918, 17.7734197168611, 11.0327071261127, 51.4377708046231, 
26.8335341704078, 9.81126144807786, 43.993699422339, 20.5123583010864, 
14.9305799969006, 23.8019575944636, 39.1543961388525, 30.4534046472982, 
61.2751477411948, 48.0770866076928, 59.4514226955362, 42.9857548968866, 
23.0139948409051, 1.76873184926808, 33.1222371393815, 10.8652087603696, 
24.5235243474599, 62.4086231633555, 55.6522683221847, 68.8337469024118, 
48.2195318546146, 6.75986870843917, 57.7931131315418, 18.2255988919642, 
40.8185531077906, 38.066848333925, 31.8611310839187, 22.2724406518973, 
51.7982920755167, 29.2363496678881, 35.541056742426, 66.5265460675582, 
28.267403066624, 40.5209824540652, 31.8187582066748, 67.2972998009063, 
53.6718824433628, 42.6495425191242, 31.6603209995665, 44.3039192620199, 
21.6216275517363, 66.9763269643299, 36.3314134527463)), .Names = c("Quadrat", 
"Month", "PopDens"), row.names = c(NA, -108L), class = "data.frame")

2 个答案:

答案 0 :(得分:1)

mutatefactor函数一起使用

df %>% mutate(Quadrat2 =
                factor(Quadrat, levels = 1:9,
                       labels =rep(c("Old Growth", "New Growth", "Absent"),3)
                       )
              )

答案 1 :(得分:1)

如果我们使用的是recode_factor,请创建具有单独组件的list,而不是paste ed

key <- setNames(as.list(rep(c("Old Growth", "Absent", "New Growth"),
        each = 3)), c(1, 4, 9, 3, 6, 7, 2, 5, 8))
df %>% 
   mutate(Sponge = recode_factor(Quadrat,  !!! key)) %>%
   head
#  Quadrat Month  PopDens     Sponge
#1       1   Jan 65.60118 Old Growth
#2       1   Jan 18.49138 Old Growth
#3       1   Jan 12.15180 Old Growth
#4       2   Feb 68.07408 New Growth
#5       2   Feb 50.98325 New Growth
#6       2   Feb 36.86843 New Growth