Gion Mors - 5 months ago 21

R Question

From a given data frame (

`myData`

`myList`

`myList <- list(c(8,15), c(2,3))`

v1 <- c(1, 2, 3, 8, 15)

v2 <- c(3, 7, 8, 9, 10)

v3 <- c(2, 4, 5, 6, 7)

v4 <- c(8, 15, 6, 7, 9)

myData <- cbind(v1, v2, v3, v4)

Ideally the subset should consists only of

`v1`

`v4`

I tried to use the

`which`

`subset <- myData[which(myData==unlist(myList[[1]][1]) & myData==unlist(myList[[1]][2]))]`

Still, the output is an empty integer. Am I missing something in the

`which`

Many thanks for your help!

GION

Answer

I found a solution for this problem:

```
myData[, unique(which(sapply(myList, function(y) apply(myData, 2, function(x)all(y %in% x))),arr.ind = T)[, 1])]
v1 v4
[1,] 1 8
[2,] 2 15
[3,] 3 6
[4,] 8 7
[5,] 15 9
```

It is a bit a ugly function therefore the explanations: The apply function checks whether all items from a list item from myList could be found in a column of myData. The sapply function ensures a search to all the items from the list. The which statements checks which he could found and gives the row and the column. We are only interested in the unique rows that are found which outputs the columns. A bit complicated but look at it hopefully it helps:)