Reputation: 2884
I have a data.frame "df":
df
id.number names
1 102 Program ID 1
2 102 Program ID 1
3 11 Program ID 1
4 11 Program ID 1
5 1293 Program ID 1
6 1293 Program ID 1
7 132 Program ID 1
8 143 Program ID 1
9 143 Program ID 1
10 143 Program ID 1
11 143 Program ID 22
12 143 Program ID 22
13 143 Program ID 22
14 155 Program ID 22
15 155 Program ID 22
16 1552 Program ID 22
17 1553 Program ID 22
And vector: name_1 <- "Program ID 1"
Now I would like to find the unique length of "id.number" by "Program ID 1" and by matching with vector "name_1"
.
I can find the unique length of "id.number" by "Program ID 1" with
length(unique(id.number[names=="Program ID 1"]))
But I would like to find unique length by matching my vector with column "names" in the data.frame "df".
So I need (and would like) to use something like that:
length(unique(subset(....)
or
length(unique(subset .... %in% names_1
And my output should be the unique length/(number) in column "id.number" and not data table
nor list
.
Desired Output:
just number, and in my case this is: 5
which is also the result of code length(unique(id.number[names=="Program ID 1"])).
Upvotes: 1
Views: 104
Reputation: 18759
As stated in the comments, you just need to wrap your code that worked on vectors into a with(df, ...)
statement:
with(df, length(unique(id.number[names=="Program ID 1"])))
Or to generalize to cases when name_1
contains several names:
with(df, length(unique(id.number[names%in%name_1])))
Upvotes: 1