r - Sum column if other column value equals any value in a list -
i using census live-work data 3 columns: homeblock, workblock , number of people made commute (s00). if want know lives , works in given place need do:
sum(data$s00[(data$homeblock == 42101) & (data$workblock == 42101)]) the problem need multiple blocks. know how given number of blocks. how i've done 2 blocks:
sum(data$s00[((data$homeblock == 42101000100) | (data$homeblock == 42101000200)) & ((data$workblock == 42101000100) | (data$workblock == 42101000200))] ) i have numerous vectors of varying length full of census tract numbers , want work no matter of length. wanted work this, didn't:
sum(data$s00[(data$homeblock == c(42101000100,42101000200,42101000300) & (data$workblock == c(42101000100,42101000200,42101000300))] ) can me?
you want %in%:
sum(data$s00[(data$homeblock %in% c(42101000100,42101000200,42101000300) & (data$workblock %in% c(42101000100,42101000200,42101000300))])
Comments
Post a Comment