【问题标题】:Problem with dplyr string mutation of dataset数据集的 dplyr 字符串突变问题
【发布时间】:2022-12-07 15:32:59
【问题描述】:

我在处理一个看起来像这样的数据框的简单突变时遇到了麻烦:

  interaction alphabetical
1      A pp B         ABpp
2      A pp G         AGpp
3      G pp A         AGpp
4      A pp J         AJpp
5      J pp A         AJpp
6      Q pp A         AppQ

我想使用字母列为每一行按字母顺序创建一个新的交互列。 示例:AGpp -> A pp G

我尝试使用这一行:

d <- d %>%
     mutate(
        correct_order_interaction = paste(
           unlist(strsplit(as.character(alphabetical),""))[1],
           "pp",
           unlist(strsplit(as.character(alphabetical),""))[2]
           )
         )

但是,这会导致此数据框:

  interaction alphabetical correct_order_interaction
1      A pp B         ABpp                    A pp B
2      A pp G         AGpp                    A pp B
3      G pp A         AGpp                    A pp B
4      A pp J         AJpp                    A pp B
5      J pp A         AJpp                    A pp B
6      Q pp A         AppQ                    A pp B

我不太明白为什么这不起作用。这可能不是解决问题的最佳方法,但我之前已经这样做过,而且通常效果很好。

我希望任何人都可以帮助我,如果有更好的方法来解决这个问题,请告诉我:)

非常感谢

dput 数据帧:

structure(list(interaction = c("A pp B", "A pp G", "G pp A", 
"A pp J", "J pp A", "Q pp A"), alphabetical = c("ABpp", "AGpp", 
"AGpp", "AJpp", "AJpp", "AppQ")), row.names = c(NA, 6L), class = "data.frame")

【问题讨论】:

  • 请将您的测试日期添加为dput()样本,以便我们重新创建它
  • 我的错,谢谢你的提醒 :) 我现在把它附在问题的底部

标签: r dataframe dplyr


【解决方案1】:

你可以使用str_match_all + map_chr

df %>%
  mutate(
    correct = alphabetical %>%
     str_match_all("[A-Z]") %>%
     map_chr(str_c, collapse = " pp ")
  )

【讨论】:

    【解决方案2】:
    library(tidyverse) 
    
    correct_order <- function(string) {
      string_clean <- string %>% 
        str_remove_all("[a-z]") %>% 
        str_split("") %>% 
        unlist()
      
      str_c(string_clean %>% 
              first(), "pp", string_clean %>% last(), sep = " ") 
    }
    
    df %>%  
      rowwise() %>% 
      mutate(correct = correct_order(alphabetical))
    
    # A tibble: 6 × 3
    # Rowwise: 
      interaction alphabetical correct
      <chr>       <chr>        <chr>  
    1 A pp B      ABpp         A pp B 
    2 A pp G      AGpp         A pp G 
    3 G pp A      AGpp         A pp G 
    4 A pp J      AJpp         A pp J 
    5 J pp A      AJpp         A pp J 
    6 Q pp A      AppQ         A pp Q 
    

    单线:

    df %>% 
      mutate(correct = map_chr(alphabetical, ~
                                 str_c(.x %>% 
                                         str_remove_all("[a-z]") %>% 
                                         str_split("") %>% 
                                         unlist() %>% 
                                         first(), 
                                       "pp",
                                       .x %>% 
                                         str_remove_all("[a-z]") %>% 
                                         str_split("") %>% 
                                         unlist() %>% 
                                         last(), 
                                       sep = " ")
                               ))
    

    【讨论】:

      猜你喜欢
      • 2018-12-03
      • 1970-01-01
      • 2018-03-04
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      • 2014-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多