Here is an example of how you could do it:
# example data frame
testdata <- data.frame(colA=c("this is","a test"),colB=c("is","a"),stringsAsFactors=FALSE)
# adding the new column
newcol <- sapply(seq_len(nrow(testdata)),function(x) gsub(testdata[x,"colB"],"",testdata[x,"colA"],fixed=TRUE))
new.testdata <- transform(testdata,colC=newcol)
# result
new.testdata
# colA | colB | colC
# --------------------------
# 1 this is | is | th
# 2 a test | a | test
EDIT: gsub(str1,'',str2,fixed=TRUE)
deletes all occurrences of str1
within str2
whereas using sub
would only delete the first occurrence. Since str1
is usually interpreted as regular expression, it is important to set fixed=TRUE
. Otherwise a mess happens if str1
contains characters such as .\+?*{}[]
. To address the comment, the following would replace only the last occurrence of str1
in str2
leading to the desired output:
revColA <- lapply(testdata[["colA"]],function(x) paste0(substring(x,nchar(x):1,nchar(x):1)))
revColA <- lapply(revColA,paste,collapse='')
revColB <- lapply(testdata[["colB"]],function(x) paste0(substring(x,nchar(x):1,nchar(x):1)))
revColB <- lapply(revColB,paste,collapse='')
revNewCol <- sapply(seq_len(nrow(testdata)),function(x) sub(revColB[x],"",revColA[x],fixed=TRUE))
newcol <- lapply(revNewCol,function(x) paste0(substring(x,nchar(x):1,nchar(x):1)))
newcol <- sapply(newcol,paste,collapse='')
new.testdata <- transform(testdata,colC=newcol)
### output ###
# colA colB colC
------------------------
# 1 |this is | is | this
# 2 | a test | a | test