简体   繁体   中英

Delete rows if one column entry of a dataframe is a substring of another column entry

I have a dataframe with two columns, V1 and V2, with entries such as A1, A2, A1+A2, A3, in both columns.

I want to delete rows if either column contains a substring of the other. So, for example, I would want to delete rows like this:

A1, A1+A2

A1+A2,A1

but not rows like this:

A1+A2, A3

I am currently using this code:

subset(dat, !dat$V1 %in% dat$V2)

but this code gets rid of rows like A1/B1, A2-B2 and A 02, A4 when I want to keep those rows.

I am thinking I can use charmatch, maybe like this:

subset(dat, charmatch(dat$V1, dat$V2) == "NA")

but this returns an empty dataframe.

When I run this code to check what charmatch would get rid of:

trial <- subset(dat, charmatch(dat$V1, dat$V2) != "NA")

rows such as A1/B1, A2-B2 and A 02, A4 appear when I want to keep those rows.

I think the problem might be in that A 02 has a space, but am not sure how to resolve this.

I also thought about using grep/grepl and regular expressions, but am not sure how this would look syntactically when I am searching one column's expression against another column. Would I convert the first column into a vector and use:

subset(dat, !grepl(V1vector, dat$V2)) 

?

Any ideas?

Here is some of the dataset:

V1          V2
A3-B3   B3  
A4/B4   A3-B3   
A 28    A 05    
A 28    A 06    
A2-B2   A2  
B 05    B1  

And this is what I would like it to look like:

V1         V2
A4/B4      A3-B3
A 28       A 05
A 28       A 06
B 05       B1

尝试这个:

df[!mapply(grepl, df$V2, df$V1),]

Minimal dataset:

f <- structure(list(V1 = c("A3-B3", "A4/B4", "A 28", "A 28", "A2-B2", 
"B 05"), V2 = c("B3", "A3-B3", "A 05", "A 06", "A2", "B1")), .Names = c("V1", 
"V2"), row.names = c(NA, -6L), class = "data.frame")

##entries of V1 that contain V2
mapply(grepl, f$V2, f$V1, MoreArgs=list(fixed=TRUE)) 
##entries of V2 that contain V1
mapply(grepl, f$V1, f$V2, MoreArgs=list(fixed=TRUE))

##combine the two negations
f[!mapply(grepl, f$V2, f$V1, MoreArgs=list(fixed=TRUE)) & 
  !mapply(grepl, f$V1, f$V2, MoreArgs=list(fixed=TRUE)),]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM