I have a dataframe as follows:
Input
one<-c("Rainy and sunny;thundering;lightning","dismal and dreary;thundering")
two<-c("Overcast;lightning","Overcast;dismal and dreary")
df2<-data.frame(one,two)
I want to compare the strings in the lists by row and extract what is the same, and what is different in new columns
The output I am expecting is:
same<-c("lightning","dismal and dreary")
different_Incol1ButNot2<-c("Rainy and sunny;thundering","thundering")
different_Incol2ButNot1<-c("Overcast","Overcast")
df2<-data.frame(one,two,same,different_Incol1ButNot2,different_Incol2ButNot1,stringsAsFactors=F)
which should output:
one two same different_Incol1ButNot2 different_Incol2ButNot1
Rainy and sunny;thundering;lightning Overcast;lightning lightning Rainy and sunny;thundering Overcast
dismal and dreary;thundering Overcast;dismal and dreary dismal and dreary thundering Overcast
so my first thought was to split and list each string:
df3$one<-as.list(strsplit(df3$one, ";"))
df3$two<-as.list(strsplit(df3$two, ";"))
However now I do not know how to compare row-wise the lists I have created within the dataframe so I guess the question is how do I make these row wise comparisons between lists of strings within a dataframe or is there an easier way to do this?