1

I am using Census live-work data with three columns: homeblock, workblock and number of people that made that commute (S00). If I want to know who lives and works in a given place I just need to do:

sum(data$S00[(data$homeblock == 42101) & (data$workblock == 42101)])

The problem is I need to do this for multiple blocks. I know how to do it for a given number of blocks. This is how I've done it for two 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 and I want this to work no matter of the length. I wanted it to work like this, but it didn't:

sum(data$S00[(data$homeblock == c(42101000100,42101000200,42101000300) &
     (data$workblock == c(42101000100,42101000200,42101000300))]
)

Can anyone help me?

4

1 回答 1

3

你想要%in%

sum(data$S00[(data$homeblock %in% c(42101000100,42101000200,42101000300) & 
     (data$workblock %in% c(42101000100,42101000200,42101000300))])
于 2013-05-20T03:28:08.773 回答