Subtract two strings from each other

MFR

I have the following input

#mydata

ID  variable1  variable2
1    a,b,c,d      c,a 
2    g,f,h        h
3    p,l,m,n,c    c,l

I wish to subtract the strings of varible2 from variable1 and I'd like to have the following output?

#Output
ID  Output 
1    b,d      
2    g,f        
3    p,m,n    

#dput

structure(list(ID = 1:3, variable1 = structure(1:3, .Label = c("a,b,c,d", 
"g,f,h", "p,l,m,n,c"), class = "factor"), variable2 = structure(c(1L, 
 3L, 2L), .Label = c("c,a", "c,l", "h"), class = "factor")), .Names =    c("ID", 
 "variable1", "variable2"), class = "data.frame", row.names = c(NA, 
-3L))
akrun

We can use Map after splitting each of the columns by , get the setdiff, paste them together, set the names of the list output with 'ID' column, stack it to 'data.frame' and set the names to 'ID' and 'Output' for the columns.

setNames(stack(setNames(Map(function(x,y) toString(setdiff(x,y)), 
         strsplit(as.character(df1$variable1), ","), 
         strsplit(as.character(df1$variable2), ",")),
              df1$ID))[2:1], c("ID", "Output"))
 #  ID  Output
 #1  1    b, d
 #2  2    g, f
 #3  3 p, m, n

Or a compact option would be

library(splitstackshape)
cSplit(df1, 2:3, ",", "long")[, .(Output = toString(setdiff(variable1, variable2))) , ID]
#   ID  Output
#1:  1    b, d
#2:  2    g, f
#3:  3 p, m, n

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Subtract two Pandas DataFrame time indexes from each other?

How to subtract two columns from each other in SQL (PostgresSQL)

How to subtract two columns of lists from each other in pandas?

How to subtract two calendar dates from each other?

MSSQL Subtract two rows from each other in union

Given two strings, find if they are one edit away from each other

Datetime in pandas dataframe will not subtract from each other

How should I subtract two vectors or matrices from each other without NumPy?

SQL Subtract two rows from each other in same column to get a result

How do I subtract two pandas datetime series DataFrames from each other when the index is a datetime?

How do I subtract two images from each other using python and opencv?

how to generate a new rows and adding 1 min after subtract two datetimes from each other

Subtract two columns, with each other as a condition, and append values

Subtract two CSV strings

How to find two strings that are close to each other

Check if two strings are permutation of each other

check if two strings are permutation of each other?

Check if two strings are rotationally equal to each other

Check if two strings are Substitution Ciphers of each other

Sum values from 2 tables and subtract it to each other

subtract each row from all other rows and view as matrix in python

How to add/subtract row values from each other in pandas?

How can I correctly subtract minutes from each other in JavaScript?

C programming: ouput two strings as two columns next to each other

Are these two model definitions differ from each other?

Is there a way to compare two Strings with each other using java streams?

Checking if two Strings are anagram of each other using basic Java

Time complexity of program determining if two strings are permutations of each other

Can two identical strings not be equal to each other in Python?