0

I have a data frame that has the 14 columns. 2 of those 14 columns are "Region" and "Population Density." Lets say that I want to find all instances when region is 4 and print out what the value of the population density is for each instance of region = 4.

here I am going to add a new column in the data frame called "PopDens" this new column will take the total population and divided it by the land region

    cdi.df$PopDens= cdi.df$TotalPop/cdi.df$LandArea
    dim(cdi.df) #here I verify the columns are now 14 and not 13
    head(cdi.df) #here I verify that the name and calculations are correct
    head(cdi.df$PopDens, 3) #here I return only the first three values 

Is there a way to return only values of pop dens when region is =4?

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • 2
    Do you want all of the columns for which region is 4? Or do you just want a vector of the population densities for which region is 4? Either way this is in no way a "complex" search. – Dason Oct 24 '17 at 18:38

1 Answers1

3

Just like this:

cdi.df[cdi.df$Region==4,] #to see the data.frame with only region 4
cdi.df$PopDens[cdi.df$Region==4] #to see only the population densities

Next time, please provide a reproducible eaxmple, as explained here.

user3640617
  • 1,546
  • 13
  • 21