r - Find length by subsetting or using %in% -
i have 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 find unique length of "id.number" "program id 1" , matching vector "name_1". can find unique length of "id.number" "program id 1" with
length(unique(id.number[names=="program id 1"])) but find unique length matching vector column "names" in data.frame "df".
so need (and like) use that:
length(unique(subset(....) or
length(unique(subset .... %in% names_1 and output should unique length/(number) in column "id.number" , not data table nor list.
desired output: number, , in case is: 5 result of code length(unique(id.number[names=="program id 1"])).
as stated in comments, need wrap code worked on vectors with(df, ...) statement:
with(df, length(unique(id.number[names=="program id 1"]))) or generalize cases when name_1 contains several names:
with(df, length(unique(id.number[names%in%name_1])))
Comments
Post a Comment