Subsetting by matching characters to column in other data-frame

7 views
Skip to first unread message

Renanel Pickholtz

unread,
Nov 27, 2016, 9:07:44 AM11/27/16
to Israel R User Group

Hello all,


I have a column within a short df containing many different names (in fact a merge of “serial-number age”):

>small_df$animals 
[1] x12288 7 x12287 1 x12532 1 x17943 1 x62618 9 x12008 5 x52147 4 x92288 3
[5004]       x12988 2 x55521 1 x12903 4 x15288 1 x52364 7 x10005 2 x12288 6
 
I’m trying to subset a different data-frame (“full_data”), so that it a specific column within the large if the name doesn’t appear in the first column it will be removed. 
 
I tried this: 
Column for criteria
>vector = as.vector((small_df$animals))
 
Keep only what matches in ‘animals’ column in the large and the column defined earlier:
 
>filtered_animals = subset(large_df [large_df$animals %in% vector,])
 But I get a new data frame with zero observation.
If I could type in the vector manually I would (e.g., == %in% c(“x12288 7”, “x12287 1”, “x12532 1”,…) I would but the list is way too long. 
 
Any help would be greatly appreciated. 

amit gal

unread,
Nov 27, 2016, 9:14:35 AM11/27/16
to israel-r-...@googlegroups.com
First, if you use the subset command then your syntz is wrong you should do something like:
filtered = subset(large,animals %in% small$animals)

also, because these are strings, make sure R didn't convert them to  factors which may mess things up. use the as.character() to undo that. e.g.
filtered = subset(large, as.character(animals) %in% as.character(small$animals))

you can also use the match command to find the exact mapping between two vectors, and then select the rows of that match

finally, as always, dplyr package gives you great tools to do what you want. in you case not only the filter command (which is similar to subset but with greater flexibility) but also the various join commands may be of help. the ROI of learning the dplyr package is huge!!!!

HTH
Amit


--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-group+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages