-1

I am trying to search for data inside of a vector. So I have two tables (turned into vectors), and I am trying to search the info of the vector "b" inside of the vector "a". Below my code is provided, does anyone knows how to fix this? I only get a TRUE/FALSE when in reallity I want to create a new vector. The column 2 of the "a" vector contains the info I am trying to search from vector "b".

a = read.table("data.txt",stringsAsFactors=FALSE,sep="\t")
a = as.vector(a[[2]])
b <- read.table("info.txt", stringsAsFactors = FALSE, sep = "\t")
b = as.vector(b[[1]])
f <- a[unlist(lapply(b, function(x) any(x %in% b)))]
Uwe
  • 41,420
  • 11
  • 90
  • 134
  • Please, [edit] your question and show the contents of `a` and `b`, at least part of. Best to add the result of `dput(a)` and `dput(b)` - Thank you. BTW: your code looks very convoluted which makes it difficult to grasp what your expected result is. So, please add the expected result as well. – Uwe May 06 '17 at 18:34
  • 1
    I'm confused about how you are using the term _vector_ when you speak of _column 2 of the "a" vector_. The result of `read.table()` is a data.frame which consists of vectors aka columns. – Uwe May 06 '17 at 19:05

1 Answers1

1

You just need to tell r to use the TRUE and FALSE to construct a subset in some way:

c<-subset(a, a%in% b)
sconfluentus
  • 4,693
  • 1
  • 21
  • 40
  • I tried but inside the vector "b" all the info concerning the vector "a" presents a different order and r is reading at vector "a" to "b" in order: a (1,2,3,4,5,6) b(5,3,2,4) So I don't know how to solve this. – Sarah Decker May 06 '17 at 16:16
  • I just ran a test with `b` out of order for `a` and it still picked all of them up...without seeing your data, it is impossible to say why you are getting the answer you get. You could use a loop... – sconfluentus May 06 '17 at 16:40