【问题标题】:R Megre Data Frame Column and RecodeR Megre 数据框列和重新编码
【发布时间】:2021-04-12 06:36:53
【问题描述】:

我有 2 个如下所示的 R 数据框:

数据帧 1:

identifier ef_posterior position_no classification
11111 0.260 1 yes
11111 0.0822 2 yes
11111 0.00797 3 yes
11111 0.04 4 no
11111 0.245 5 yes
11111 0.432 6 yes
11112 0.342 1 maybe
11112 0.453 2 yes
11112 0.0032 3 yes
11112 0.241 5 no
11112 0.0422 6 yes
11112 0.311 4 no

数据框 2:

study_identifier %LVEF
11111 62
11112 76

我想将这两个数据框合并并重新排列成这样的:

Study_identifier 和 identifier 是同一个东西(只是不同的列名)。另外,我想重新编码分类,使yes = 0,no = 1,maybe = 2

identifier pos_1 pos_1_class pos_2 pos_2_class pos_3 pos_3_class pos_4 pos_4_class pos_5 pos_5_class pos_6 pos_6_class %LVEF
11111 0.260 0 0.0822 0 0.00797 0 0.04 1 0.245 0 0.432 0 62
11112 0.342 2 0.453 0 0.0032 0 0.311 1 0.241 1 0.0422 0 76
df1 %>% mutate(position_no = paste0("position_", position_no)) %>%
  pivot_wider(id_cols = identifier, names_from = position_no, values_from = ef_posterior) %>%
  left_join(df2 %>% mutate(study_identifier = as.numeric(as.character(study_identifier))), by = c("identifier" = "study_identifier"))

这是我现在的代码,但我不知道在哪里放置分类列的代码

我该怎么做呢? 任何帮助将不胜感激!

【问题讨论】:

    标签: r dataframe merge


    【解决方案1】:

    您可以使用dplyrcase_when 轻松重新编码:

    df1 %>% mutate(
      classification = 
        case_when( classification == "yes" ~ 1,
                   classification == "no" ~ 0,
                   classification == "maybe" ~ 2)
    )
    

    我会通过以下方式解决:

    library(tidyverse)
    df1 <- data.frame(
      stringsAsFactors = FALSE,
      identifier = c(11111L,11111L,11111L,11111L,
                     11111L,11111L,11112L,11112L,11112L,11112L,11112L,
                     11112L),
      ef_posterior = c(0.26,0.0822,0.00797,0.04,
                       0.245,0.432,0.342,0.453,0.0032,0.241,0.0422,0.311),
      position_no = c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 5L, 6L, 4L),
      classification = c("yes","yes","yes","no",
                         "yes","yes","maybe","yes","yes","no","yes","no")
    )
    
    df2 <- data.frame(
      check.names = FALSE,
      study_identifier = c(11111L, 11112L),
      `%LVEF` = c(62L, 76L)
    )
    
    df1 %>% mutate(
      classification = 
        case_when( classification == "yes" ~ 1,
                   classification == "no" ~ 0,
                   classification == "maybe" ~ 2)
    ) %>% 
      pivot_wider(
        id_cols = c(identifier), names_from = c(position_no), values_from = c(classification,ef_posterior)) %>% 
    left_join(df2, by = c("identifier" = "study_identifier"))
    #> # A tibble: 2 x 14
    #>   identifier classification_1 classification_2 classification_3 classification_4
    #>        <int>            <dbl>            <dbl>            <dbl>            <dbl>
    #> 1      11111                1                1                1                0
    #> 2      11112                2                1                1                0
    #> # … with 9 more variables: classification_5 <dbl>, classification_6 <dbl>,
    #> #   ef_posterior_1 <dbl>, ef_posterior_2 <dbl>, ef_posterior_3 <dbl>,
    #> #   ef_posterior_4 <dbl>, ef_posterior_5 <dbl>, ef_posterior_6 <dbl>,
    #> #   `%LVEF` <int>
    

    由 reprex 包 (v0.3.0) 于 2021-04-12 创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      相关资源
      最近更新 更多