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
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.
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.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With