简体   繁体   English

在 dplyr 中使用 mutate 和 case_when 将新值插入到数据框中

[英]Inserting new values into a data frame using mutate and case_when in dplyr

I have the following data frame of letters with some blank (NA) slots for the lower cases我有以下字母数据框,其中包含一些用于小写字母的空白 (NA) 插槽

letters_df <- data.frame(caps = LETTERS[1:10], lows = letters[c(1,2,11,11,11,11,11,11,11,10)])
letters_df[letters_df == "k"] <- NA
letters_df

To fill in some of the blanks I am using this new data frame I constructed为了填补一些空白,我正在使用我构建的这个新数据框

new_letters <- data.frame(caps = c("C", "D", "F", "G", "H"),
                     lows = c("c", "d", "f", "g", "h"))

Following on from a previous question I am using dplyr mutate and case_when as follows继上一个问题之后,我使用 dplyr mutate 和 case_when 如下

letters_df %>%
  mutate(lows = case_when(
    caps %in% new_letters$caps ~ new_letters$lows,
    TRUE ~ lows))

However, the result does not add in the missing letters and throws an error asking for a vector of the same length as the letters_df column.但是,结果不会添加丢失的字母,并会抛出一个错误,要求提供与 letters_df 列长度相同的向量。 I thought I had a good handle on the syntax here.我以为我已经很好地掌握了这里的语法。 Can help me with where I am going wrong?可以帮我解决我哪里出错了吗?

You could consider using a left_join combined with coalesce :您可以考虑将left_joincoalesce结合使用:

library(dplyr)

letters_df %>% 
  left_join(new_letters, by = "caps") %>% 
  mutate(lows = coalesce(lows.x, lows.y), .keep = "unused")

This returns这返回

   caps lows
1     A    a
2     B    b
3     C    c
4     D    d
5     E <NA>
6     F    f
7     G    g
8     H    h
9     I <NA>
10    J    j

As an alternative that is more similar to your approach, you could transform your new_letters data.frame into a lookup vector returning the same result:作为与您的方法更相似的替代方法,您可以将new_letters data.frame 转换为返回相同结果的查找向量:

lookup <- tibble::deframe(new_letters)

letters_df %>% 
  mutate(lows = case_when(caps %in% names(lookup) ~ lookup[caps],
                          TRUE ~ lows))

This is a typical case that rows_* from dplyr can treat:这是 dplyr 中的dplyr rows_*可以处理的典型情况:

library(dplyr)

letters_df %>%
  rows_patch(new_letters, by = "caps")

   caps lows
1     A    a
2     B    b
3     C    c
4     D    d
5     E <NA>
6     F    f
7     G    g
8     H    h
9     I <NA>
10    J    j

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

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