简体   繁体   English

在 R 中添加一个包含 tibble 特定列的缺失值的列

[英]Adding a column that contain te missing values of an specific column of a tibble in R

I am working with R.我正在使用 R。 I got the missing values of a specific column in a dataset and I need add them into my main data.我得到了数据集中特定列的缺失值,我需要将它们添加到我的主数据中。

My data looks like this...我的数据看起来像这样......

A           B    C    D    G
Joseph      5    2.1  6.0  7.8
Juan        NA   3.0  3.5  3.8
Miguel      2    4.0  2.0  2.5
Steven      NA   6.0  5.0  0.2
Jennifer    NA   0.1  5.0  7.0
Emma        8.0  8.1  8.3  8.5

So, no I have the data of the missing values in column B所以,不,我有 B 列中缺失值的数据

A          B
Juan       3.0
Steven     2.5
Jennifer   4.4

I need to add them into my main data.我需要将它们添加到我的主要数据中。 I tried to use the function coalesce that it is within tidyverse, but I wasn't able to get the right result.我尝试使用 function 合并它在 tidyverse 内,但我无法获得正确的结果。

One option could be:一种选择可能是:

df %>%
 mutate(B = if_else(is.na(B), df2$B[match(A, df2$A)], B))

         A   B   C   D   G
1   Joseph 5.0 2.1 6.0 7.8
2     Juan 3.0 3.0 3.5 3.8
3   Miguel 2.0 4.0 2.0 2.5
4   Steven 2.5 6.0 5.0 0.2
5 Jennifer 4.4 0.1 5.0 7.0
6     Emma 8.0 8.1 8.3 8.5

Does this work:这是否有效:

df
# A tibble: 6 x 5
  A            B     C     D     G
  <chr>    <dbl> <dbl> <dbl> <dbl>
1 Joseph       5   2.1   6     7.8
2 Juan        NA   3     3.5   3.8
3 Miguel       2   4     2     2.5
4 Steven      NA   6     5     0.2
5 Jennifer    NA   0.1   5     7  
6 Emma         8   8.1   8.3   8.5
dd
# A tibble: 3 x 2
  A            B
  <chr>    <dbl>
1 Juan       3  
2 Steven     2.5
3 Jennifer   4.4
df$B[match(dd$A,df$A)] <- dd$B
df
# A tibble: 6 x 5
  A            B     C     D     G
  <chr>    <dbl> <dbl> <dbl> <dbl>
1 Joseph     5     2.1   6     7.8
2 Juan       3     3     3.5   3.8
3 Miguel     2     4     2     2.5
4 Steven     2.5   6     5     0.2
5 Jennifer   4.4   0.1   5     7  
6 Emma       8     8.1   8.3   8.5

You can join the two dataframe and use coalesce for B values.您可以连接两个 dataframe 并对B值使用coalesce

library(dplyr)

df1 %>%
  left_join(df2, by = 'A') %>%
  mutate(B = coalesce(B.x, B.y)) %>%
  select(names(df1))

#         A   B   C   D   G
#1   Joseph 5.0 2.1 6.0 7.8
#2     Juan 3.0 3.0 3.5 3.8
#3   Miguel 2.0 4.0 2.0 2.5
#4   Steven 2.5 6.0 5.0 0.2
#5 Jennifer 4.4 0.1 5.0 7.0
#6     Emma 8.0 8.1 8.3 8.5

Or in base R:或者在基础 R 中:

transform(merge(df1, df2,  all.x = TRUE, by = 'A'), 
          B = ifelse(is.na(B.x), B.y, B.x))[names(df1)]

You can to join the data and then apply the value for NA value on column B.您可以加入数据,然后在 B 列上应用 NA 值的值。


# your original data with missing value in column B
data

# data that contain data to fill into column B
additional_data

library(dplyr)
merged_data <- left_join(data, additional_data, by = "A",
 suffix = c("", "_additional"))

merged_data %>% mutate(B = if_else(is_na(B), B_additional, B)) %>%
  select(-B_additional)

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

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