【问题标题】:R dplyr/tidyr recode column valuesR dplyr/tidyr 重新编码列值
【发布时间】:2018-10-08 22:00:04
【问题描述】:

我有多个数据集,我使用 rbind 合并到 1 个 dplyr 数据帧中。

GapAnalysis16 <- select(memSat16,
     importance_communication_website_content, 
     satisfaction_communication_website_content,
     status,
     Year2016) %>% 
     rename(ComImpt=importance_communication_website_content, 
     ComSat = satisfaction_communication_website_content,
     status = status,
     year = Year2016)


 GapAnalysis17July <- select(memSatJuly17, 
    importance_communication_website_content_JULY17,
    satisfaction_communication_website_content_JULY17, 
    role_primary_new_JULY17,Year2017_July) %>% 
    rename(ComImpt=importance_communication_website_content_JULY17, 
    ComSat = satisfaction_communication_website_content_JULY17,
    status = role_primary_new_JULY17,
    year = Year2017_July)


 GapAnalysis <- rbind(GapAnalysis17July,GapAnalysis16)

得到了我的新组合数据集:

   ComImpt ComSat status year
1       4      2      1    1
2      NA     NA      1    1
3       4      5      5    1
4       3      3      5    1
5       6      6      5    1
6       5      5      1    1

我需要它的长格式,所以转换它:

    GapAnalysis_LongForm <-  GapAnalysis %>%
    gather(key = Product,value = Score, ComSat, ComImpt)

现在有了这个:

    status  year Product Score
     <dbl> <dbl> <chr>   <dbl>
 1     1.    1. ComSat      2.
 2     5.    1. ComSat      5.
 3     5.    2. ComSat      3.
 4     1.    1. ComSat      5.
 5     1.    1. ComImpt     4.
 6     5.    1. ComSat      4.

我现在需要将 ComSat 和 ComImpt 重新编码为值(1 和 2),但我很难过。 Recode 和 recode_factor 给了我错误。我正在尝试获得类似这样的输出:

    status  year Product Score
     <dbl> <dbl> <chr>   <dbl>
 1     1.    1. 1           2.
 2     5.    1. 1           5.
 3     5.    2. 1           3.
 4     1.    1. 1           5.
 5     1.    1. 2           4.
 6     5.    1. 1           4.

在正确的方向上的任何一般点?

我很感激!!!

【问题讨论】:

  • 您参考但从不提供不起作用的代码,也不提供实际的输出/错误;如果您希望获得任何相关帮助,我建议您将这个问题多一点reproducible
  • 并删除与问题不直接相关的代码,描述预期输出。

标签: r dplyr tidyr recode key-pair


【解决方案1】:

您遇到了一些问题,因为您在mutate 之外使用recode_factor。当您修改数据框的列时,请确保使用mutate(在tidyverse 的上下文中)。

以下应该可以工作并做同样的事情。


带有基础factor函数

df %>%
  mutate(Product = factor(Product, levels = c("ComSat", "ComImpt"), labels = c(1L, 2L)))

带有recode_factor功能

df %>%
  mutate(Product = recode_factor(Product, "ComSat" = 1L, "ComImpt" = 2L))

df3 <- df %>%
  mutate_at(vars(Product), ~recode_factor(.,"ComSat" = 1L, "ComImpt" = 2L))

【讨论】:

    【解决方案2】:

    如果您的 data.frame 中只有 2 个 Product 代码 (ComSat,ComImpt),那么简单的 ifelse 将更容易提供帮助。

    您需要在dplyr 链中添加额外步骤:mutate(Product = ifelse(Product=="ComSat", 1L, 2L))

    GapAnalysis_LongForm  <- GapAnalysis %>%
      gather(key = Product,value = Score, ComSat, ComImpt) %>%
      mutate(Product = ifelse(Product=="ComSat", 1L, 2L))
    
    #    status year Product Score
    # 1       1    1       1     2
    # 2       1    1       1    NA
    # 3       5    1       1     5
    # 4       5    1       1     3
    # 5       5    1       1     6
    # 6       1    1       1     5
    # 7       1    1       2     4
    # 8       1    1       2    NA
    # 9       5    1       2     4
    # 10      5    1       2     3
    # 11      5    1       2     6
    # 12      1    1       2     5
    

    【讨论】:

      【解决方案3】:

      修改@hpesoj626的mutate_at方法:

      根据 tidyverse,作用域动词(_if、_at、_all)已被现有动词中的 cross() 使用取代(有关更多信息,请参阅 here)。

      以下代码应该可以工作:

      df3 <- df %>%
        mutate(across(Product), ~recode_factor(.,"ComSat" = 1L, "ComImpt" = 2L))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-21
        • 1970-01-01
        • 2021-05-19
        • 1970-01-01
        • 2018-08-06
        • 2018-05-11
        • 2015-05-23
        相关资源
        最近更新 更多