【问题标题】:How to apply "mutate" to each element of the column when elements differ from each other?当元素彼此不同时,如何将“mutate”应用于列的每个元素?
【发布时间】:2019-09-27 03:30:52
【问题描述】:

我已经解析了一个语料库,我现在有这样的东西,来自 text1

  doc_id sentence_id token_id        token        lemma   pos   entity
1  text1           1        1 Introductory introductory   ADJ         
2  text1           1        2    statement    statement  NOUN         
3  text1           1        3         with         with   ADP         
4  text1           1        4          Q&A          Q&A PROPN    ORG_B
5  text1           1        5         \n           \n   SPACE         
6  text1           1        6         Jean         Jean PROPN PERSON_B

text100

doc_id sentence_id token_id token  lemma   pos entity
758712 text100         273       32  task   task  NOUN       
758713 text100         273       33     .      . PUNCT       
758714 text100         273       34              SPACE       
758715 text100         274        1 Thank  thank  VERB       
758716 text100         274        2   you -PRON-  PRON       
758717 text100         274        3     .      . PUNCT   

现在,我正在尝试更改 doc_id 列的每个元素,以便只要有 text1,它就会出现 text134,只要有是text2就会出现text135等等(基本上从1-100到134-233)。

我只是使用以下代码为 text1 做的:

parse2 %>% mutate(doc_id = recode(doc_id, text1 = "text134"))

如何将相同的逻辑应用于从 1 到 100 的每个文本?试图创建一些循环,但我对 R 很陌生,我只是坚持下去。

你能帮帮我吗?

非常感谢!

【问题讨论】:

    标签: r loops dplyr


    【解决方案1】:

    doc_id中删除文本,这样你就只剩下数字了,然后加上133得到new_id

    transform(df, new_id = paste0("text", as.integer(sub("\\D+", "", doc_id)) + 133))
    #Or if it is always "text" in doc_id remove that directly
    #transform(df, new_id = paste0("text", as.integer(sub("text", "", doc_id)) + 133))
    
    #  id doc_id  new_id
    #1  1  text1 text134
    #2  2  text1 text134
    #3  3  text1 text134
    #4  4  text2 text135
    #5  5  text2 text135
    #6  6  text2 text135
    #7  7  text3 text136
    #8  8  text3 text136
    #9  9  text3 text136
    

    数据

    df <- structure(list(id = 1:9, doc_id = structure(c(1L, 1L, 1L, 2L, 
    2L, 2L, 3L, 3L, 3L), .Label = c("text1", "text2", "text3"), 
    class = "factor")), class = "data.frame", row.names = c(NA, -9L))
    

    【讨论】:

      【解决方案2】:

      Ronak 的数据

      df %>%
          mutate(doc_id = paste0("text", 133 + as.integer(gsub("text(\\d+)", "\\1", doc_id))))
      #  id  doc_id
      #1  1 text134
      #2  2 text134
      #3  3 text134
      #4  4 text135
      #5  5 text135
      #6  6 text135
      #7  7 text136
      #8  8 text136
      #9  9 text136
      

      【讨论】:

        【解决方案3】:

        我们可以使用来自stringrstr_c 以及来自readrparse_number

        library(stringr)
        library(readr)
        library(dplyr)
        df1 %>%
             mutate(new_id = str_c("text", parse_number(doc_id) + 133))
        #  id doc_id  new_id
        #1  1  text1 text134
        #2  2  text1 text134
        #3  3  text1 text134
        #4  4  text2 text135
        #5  5  text2 text135
        #6  6  text2 text135
        #7  7  text3 text136
        #8  8  text3 text136
        #9  9  text3 text136
        

        或者另一种选择是通过matching 与unique 元素将'doc_id' 转换为索引,添加133 和paste 与'text'

        df1 %>%
            mutate(doc_id = str_c('text', 133 + match(doc_id, unique(doc_id))))
        

        也可以和base R一起使用

        df1$doc_id <- with(df1, paste0('text', 133 + match(doc_id, unique(doc_id))))
        

        数据

        df1 <- structure(list(id = 1:9, doc_id = c("text1", "text1", "text1", 
        "text2", "text2", "text2", "text3", "text3", "text3")), row.names = c(NA, 
        -9L), class = "data.frame")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-15
          • 1970-01-01
          • 2014-01-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-14
          • 1970-01-01
          相关资源
          最近更新 更多