2017-10-24 6 views
0

14 개의 열을 가진 데이터 프레임이 있습니다. 14 개의 열 중 2 개가 "지역"과 "인구 밀도"입니다. 내가 지역을 4로했을 때 모든 인스턴스를 찾고 인구 밀도의 값이 각 지역의 인스턴스 = 4에 대해 무엇을 인쇄하고 싶은 지 말하십시오.R의 데이터 프레임에서 복잡한 검색을 수행하는 방법은 무엇입니까?

여기 데이터 프레임에 새 열을 추가 할 것입니다. "PopDens" 이 새로운 열은 지역 = 4 일 때 팝 굴의 값을 반환하는 방법이 있나요 전체 인구를 가지고 땅 지역

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 

하여 나눈 것입니까? 다만이 같은

+2

영역이 4 인 모든 열을 원 하시겠습니까? 또는 지역이 4 인 인구 밀도 벡터를 원하십니까? 어느 쪽이든 이것은 결코 "복잡한"검색이 아닙니다. – Dason

답변

3

: here 설명으로

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 

다음 시간, 재현 eaxmple을 제공하십시오.