r - Subsetting a Data Table using %in% -


a stylized version of data.table is

outmat <- data.table(merge(merge(1:5, 1:5, all=true), 1:5, all=true)) 

what select subset of rows data.table based on whether value in 1st column found in of other columns (it handling matrices of unknown dimension, can't use sort of "row1 == row2 | row1 == row3"

i wanted using

output[row1 %in% names(output)[-1], ] 

but ends returning true if value in row1 found in of rows of row2 or row3, not intended behavior. there sort of vectorized version of %in% achieve desired result?

to elaborate, want enumeration of 3-tuples set 1:5, drawn replacement, such first value same either second or third value, like:

1 1 1
1 1 2
1 1 3
1 1 4
1 1 5
...
2 1 2
2 2 1
...
5 5 5

what code instead gives me every enumeration of 3-tuples, checking whether first digit (say, 5), ever appears anywhere in 2rd or 3rd columns, not within same row.

one option construct expression , evaluate it:

dt = data.table(a = 1:5, b = c(1,2,4,3,1), c = c(4,2,3,2,2), d = 5:1) #   b c d #1: 1 1 4 5 #2: 2 2 2 4 #3: 3 4 3 3 #4: 4 3 2 2 #5: 5 1 2 1  expr = paste(paste(names(dt)[-1], collapse = paste0(" == ", names(dt)[1], " | ")),              "==", names(dt)[1]) #[1] "b == | c == | d == a"  dt[eval(parse(text = expr))] #   b c d #1: 1 1 4 5 #2: 2 2 2 4 #3: 3 4 3 3 

another option loop through , compare columns:

dt[rowsums(sapply(dt, '==', dt[[1]])) > 1] #   b c d #1: 1 1 4 5 #2: 2 2 2 4 #3: 3 4 3 3 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -