Reputation: 493
I have these two variables (itemname and name) which contain this data. I want to subset only those names from name variable where the name is equal to Itemname. The output should look like this because it only subsets those values from Itemname which are present in name.
X3
X5
X7
X57
X66
X69
I have tried
> Itemname %in% name
logical(0)
The class of both variables is:-
> class(Itemname)
[1] "matrix" "array"
> class(name)
[1] "character"
Another example, if above example wasn't clear lets say that X has values A,B,C,D and Y has values ABCDEF, then the output of Y should look like A, B,C, D because it only subsetted the values which exist in X.
Upvotes: 0
Views: 259
Reputation: 1298
If you make your variable Itemname
a vector e.g. Itemname <- c("X3", "X5", "X7", "X57", "X66", "X69")
, you can do the following:
result <- name[name %in% Itemname]
Upvotes: 1