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を提供してください。

関連する問題