0

I have a data frame containing id and other variables, and also a list which has some ids. Now I want to extract row of ids from the data frame which has same ids in list.

data frame

id   value  time
1     12     1.0
1     14     1.6
4     18     2.0
6     9      3.6
3     11     4.2
5     12     0.8

list

1,3,4

Result

id   value  time
1     12     1.0
1     14     1.6
3     11     4.2
4     18     2.0
Sotos
  • 51,121
  • 6
  • 32
  • 66
  • Use `%in%`... We need to see the structure of your list to be able to help further. In general, please make sure your examples are [reproducible](http://stackoverflow.com/questions/5963269) – Sotos Sep 03 '19 at 07:17

1 Answers1

1

As @Sotos explained, that could be done as following using %in%:

Data[Data$id %in% list,]
# id value time
# 1:  1    12  1.0
# 2:  1    14  1.6
# 3:  4    18  2.0
# 4:  3    11  4.2
Carles
  • 2,731
  • 14
  • 25
  • do you use `data.table`. The format looks not as a base `data.frame`? – Roman Sep 03 '19 at 07:33
  • FYI, I rejected your edit at the question as you are making an assumption that the list mentioned by OP is not a list but a vector. This can clearly conflict with OPs real object. This is also the reason that I did not dupe this question. Waiting for clarification – Sotos Sep 03 '19 at 07:42
  • 1
    Hello @sotos. I agree with you. Given how he wrote it, assumed it was a vector given that otherwise he would have copied as he did with the table the result of R as [[1]] 1 [[2]] 3 etc. And given the accepted answer, I assumed that was the way. Nevertheless, i totally understand your point and probably it would have been better to ask him/her. – Carles Sep 03 '19 at 07:51