简体   繁体   English

插补后如何加入列

[英]How to join to column after imputation

I run multiple imputation to impute missing data for 2 variables of a data frame, then I get a new data frame (with 2 columns for 2 imputed variables).我运行多重插补来插补数据框的 2 个变量的缺失数据,然后我得到一个新的数据框(2 列用于 2 个插补变量)。

Now I want to replace the 2 columns in the original data frame with the two newly imputed columns from my new dataframe.现在我想用我的新 dataframe 中的两个新估算的列替换原始数据框中的 2 列。 What should I do?我应该怎么办?

Original data frame new data frame for imputed variables原始数据框插补变量的新数据框

Since there's no id column in the new dataframe, you can just mutate to replace the columns in the original dataframe with the output from the new dataframe. Since there's no id column in the new dataframe, you can just mutate to replace the columns in the original dataframe with the output from the new dataframe.

library(tidyverse)

df_orig %>% 
  dplyr::mutate(ABV = df_new$ABV, EBC = df_new$EBC) 

Output Output

  id ABV EBC    third
1  1 -61 -58 37.94029
2  2 -80 -67 47.81479
3  3 -62 -66 48.85903
4  4 -69 -78 23.18026
5  5 -51 -77 29.91952

Data数据

df_orig <-
  structure(
    list(
      id = c(1, 2, 3, 4, 5),
      ABV = c(
        38.9932923251763,
        20.0923723727465,
        37.640398349613,
        31.4673039061017,
        49.192731983494
      ),
      EBC = c(
        42.341671793256,
        32.936319950968,
        33.8184517389163,
        21.5938150603324,
        22.8182014194317
      ),
      third = c(
        37.9402944352478,
        47.8147878032178,
        48.8590325415134,
        23.1802612892352,
        29.9195193173364
      )
    ),
    class = "data.frame",
    row.names = c(NA,-5L)
  )

df_new <-
  structure(
    list(
      ABV = c(-61,-80,-62,-69,-51),
      EBC = c(-58,-67,-66,-78,-77)
    ),
    class = c("rowwise_df", "tbl_df", "tbl",
              "data.frame"),
    row.names = c(NA,-5L),
    groups = structure(
      list(.rows = structure(
        list(1L, 2L, 3L, 4L, 5L),
        ptype = integer(0),
        class = c("vctrs_list_of",
                  "vctrs_vctr", "list")
      )),
      row.names = c(NA,-5L),
      class = c("tbl_df",
                "tbl", "data.frame")
    )
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM