Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove rows from table based on strings in column

Tags:

r

I would like to remove rows from the table using the strings stored in the vector:

> dput(vec_temp)
c("HL", "LL", "ML")

Table looks like that:

structure(list(Accession = "AT1G71220", variable = structure(14L, .Label = c("f:HL-f:FL", 
"f:LL-f:FL", "f:ML-f:FL", "f:LL-f:HL", "f:ML-f:LL", "f:ML-f:HL", 
"m:HL-m:FL", "m:LL-m:FL", "m:ML-m:FL", "m:LL-m:HL", "m:ML-m:HL", 
"m:ML-m:LL", "ntrc:HL-ntrc:FL", "ntrc:LL-ntrc:FL", "ntrc:ML-ntrc:FL", 
"ntrc:LL-ntrc:HL", "ntrc:ML-ntrc:HL", "ntrc:ML-ntrc:LL", "WT:HL-WT:FL", 
"WT:LL-WT:FL", "WT:ML-WT:FL", "WT:LL-WT:HL", "WT:ML-WT:HL", "WT:ML-WT:LL"
), class = "factor"), value = 0.0445054204080209), .Names = c("Accession", 
"variable", "value"), row.names = 2167L, class = "data.frame", na.action = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 15L, 16L, 
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L), .Names = c("35", "199", 
"363", "527", "691", "855", "1019", "1183", "1347", "1511", "1675", 
"1839", "2003", "2331", "2495", "2659", "2823", "2987", "3151", 
"3315", "3479", "3643", "3807"), class = "omit"))

Please focus on the column named variable. This table contains a single row with variable -> ntrc:LL-ntrc:FL. Looking at the vector from the begining of this thread we can see that FL was not there. Thus, I would like to remove that row (in general whole table). I would like to ask R to compare the strings from the vector with the strings stored in the column named variable which appear after :. If strings from table cannot be find in the vector the whole row (or table) should be removed.

like image 549
tralala Avatar asked Apr 27 '26 05:04

tralala


1 Answers

An idea is to split the variable column and use grepl to identify whether the suffixes in the vector appear in the string both times (hence the == 2), i.e.

vec_temp <- c("HL", "LL", "ML")

i1 <- colSums(sapply(strsplit(as.character(df$variable), '-', fixed = TRUE), function(i) 
                              grepl(paste(vec_temp, collapse = '|'), i))) == 2

df[i1,]
like image 62
Sotos Avatar answered Apr 29 '26 20:04

Sotos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!