【问题标题】:argument 'pattern' has length > 1 and only the first element will be used - GSUB()参数 'pattern' 的长度 > 1 并且仅使用第一个元素 - GSUB()
【发布时间】:2017-05-23 14:28:51
【问题描述】:

我有以下问题。

table <- data.frame(col1 = c("cars1 gm", "cars2 gl"), col2 = c("cars1 motor mel", "cars2 prom del"))

      col1            col2
1 cars1 gm cars1 motor mel
2 cars2 gl  cars2 prom del

table$word <- gsub(table$col1, ' ', table$col2) 

Warning message:  In gsub(table$col1, " ", table$col2) :  argument
'pattern' has length > 1 and only the first element will be used

如何创建一个名为word 的新列,其中只包含col2 中没有出现在col1 中的那些值?

      col1            col2       word
1 cars1 gm cars1 motor mel  motor mel
2 cars2 gl  cars2 prom del   prom del

【问题讨论】:

    标签: r dataframe


    【解决方案1】:

    您可以使用gsub 构建查找,然后在列上使用sapply 来执行感兴趣的gsub

    table$col1 <- gsub(" ", "|", table$col1)
    table$word <- sapply(1:nrow(table), function(x) gsub(table$col1[x], "", table$col2[x]))
    
    table
    #      col1            col2       word
    #1 cars1|gm cars1 motor mel  motor mel
    #2 cars2|gl  cars2 prom del   prom del
    

    使用与上述答案类似的想法,但使用mapply 而不是sapply

    table$word <- mapply(function(x, y) gsub( gsub(" ", "|", x), "", y),
                                        table$col1,
                                        table$col2)
    

    【讨论】:

      【解决方案2】:

      你可以使用mapply

      #Make sure you read your data with stringsAsFactors = FALSE, 
      table<-data.frame(col1=c("cars1 gm","cars2 gl"),
                        col2=c("cars1 motor mel", "cars2 prom del"), stringsAsFactors = FALSE)
      
      table$word <- mapply(function(x, y) 
                           trimws(gsub(sapply(strsplit(x, ' '), paste, collapse = '|'), '', y)), 
                           table$col1, table$col2)
      table
      #      col1            col2      word
      #1 cars1 gm cars1 motor mel motor mel
      #2 cars2 gl  cars2 prom del  prom del
      

      【讨论】:

        【解决方案3】:

        您可以像这样使用mapplypastestrsplit

        table$word <- mapply(function(x, y) paste(y[!(y %in% x)], collapse=" "),
                             strsplit(as.character(table$col1), split=" "),
                             strsplit(as.character(table$col2), split=" "))
        

        这里,strsplit 在 " " 上分割一个字符向量并返回一个列表。这两个列表被馈送到mapply,它检查每个列表的相应值并返回第二个列表中不在第一个列表中的值。生成的向量与paste 及其折叠参数一起粘贴。

        返回

        table
              col1            col2      word
        1 cars1 gm cars1 motor mel motor mel
        2 cars2 gl  cars2 prom del  prom del
        

        【讨论】:

          【解决方案4】:

          您可以拆分col1col2中的字符串,因为单词的顺序可能不同,然后您可以使用setdiff选择仅出现在col2中的单词:

          table$word=sapply(1:nrow(table),function(i)
          paste(setdiff(unlist(strsplit(table$col2[i]," ")),
          unlist(strsplit(table$col1[i]," "))),collapse=" "))
          

          这会返回:

          col1            col2      word
          1 cars1 gm cars1 motor mel motor mel
          2 cars2 gl  cars2 prom del  prom del
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-06-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多