mks mks - 1 year ago 139
R Question

R: How to use setdiff on two string vectors by only comparing the first 3 tab delimited items in each string?

I am trying to figure out a way in R to take the difference of two string vectors, but only based on the first 3 columns that are tab delimited in each string. For Example this is list1 and list2





i want to do
, so that i just get everything in list2 that is non-existent in list1, however i want to do it based on just the first 3 tab delimited strings. So in list1 i would only consider:


from the first entry. However i still want the full string returned. I only want to compare using the first 3 columns. I am having trouble figuring out how to do this, any help would be appreciated. Ive already looked at several SO posts, none of them seemed to help.

Answer Source

For extracting the first three columns (not sure why you need this as a long string rather than a dataframe...), I would use beg2char() from the qdap library. (Although, if they are all the same length base substr() will work fine.)

beg2char(list1, '\t', 3) # Will extract from the beginning up to the third tab delimiter

Then rather than setdiff I would simply use %in% to check if the substring of the element in list2 matches any of the elements in list1.

beg2char(list2, '\t', 3) %in% beg2char(list1, '\t', 3) # will give you TRUE/FALSE
list2[!(beg2char(list2, '\t', 3) %in% beg2char(list1, '\t', 3))]

Will give the the full elements of list2 that have substring that are nonexistent in list1.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download