Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

argument 'pattern' has length > 1 and only the first element will be used - GSUB()

Tags:

dataframe

r

I have the following problem.

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

How do I create a new column called word containing only those values of col2 which do not appear in col1?

      col1            col2       word
1 cars1 gm cars1 motor mel  motor mel
2 cars2 gl  cars2 prom del   prom del
like image 263
lolo Avatar asked May 23 '17 14:05

lolo


People also ask

Has length 1 and only the first element will be used?

How to Fix in R: the condition has length > 1 and only the first element will be used. This error occurs when you attempt to use an if() function to check for some condition, but pass a vector to the if() function instead of individual elements. This tutorial shares exactly how to fix this error.

What does the condition has length 1 and only the first element will be used mean in R?

This warning message occurs when you pass a function a multi-value argument (such as a vector or an array) and the function expects to evaluate a single value. R will print this warning message result if you attempt to pass a multi-element vector to a function which can operate on only one logical element.


2 Answers

You can use gsub to build your lookup and then sapply over columns to perform the gsub of interest:

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

Using a similar idea as the above answer but using mapply instead of sapply:

table$word <- mapply(function(x, y) gsub( gsub(" ", "|", x), "", y),
                                    table$col1,
                                    table$col2)
like image 92
Mike H. Avatar answered Oct 20 '22 15:10

Mike H.


You can use 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
like image 38
Sotos Avatar answered Oct 20 '22 13:10

Sotos