2016-09-19 13 views
0

2つのデータフレームがあるとします。私はすべてのデータフレームから業界のc3と言う行を選択し、別のdata.frame、df3に配置したいと考えています。私の実際のデータでn個のデータフレームから1行を取り出し、新しいデータフレームを作成します。R

df1 = data.frame(Industries = c("c1", "c2", "c3", "c4", "c5"), Exports = c(100, 40, 30, 10, 50), Imports =c(90,50,25,15,50)) 
df2 = data.frame(Industries = c("c1", "c2", "c3", "c4", "c5"), Exports = c(20, 90, 10, 30, 60), Imports =c(40,30,25,55,40)) 

df1 
     Industries Exports Imports 
    1   c1  100  90 
    2   c2  40  50 
    3   c3  30  25 
    4   c4  10  15 
    5   c5  50  50 
df2 
     Industries Exports Imports 
    1   c1  20  40 
    2   c2  90  30 
    3   c3  10  25 
    4   c4  30  55 
    5   c5  60  40 

私は、16種類のデータフレームを持っているので、可能な場合にコードが作成される別の1、16個のデータフレームから16行を置くことを許可する必要があります。

+0

'行うに「C3」を有する行のサブセット.call(rbind、lapply(list(df1、df2)、サブセット、Industries == "c3")) '? – Psidom

答えて

0

あなたはdplyrとpurrrパッケージで次の操作を行うことができ、あなたの列は常に同じであると仮定すると:

library(purrr) 
library(dplyr) 

df1 = data.frame(Industries = c("c1", "c2", "c3", "c4", "c5"), Exports = c(100, 40, 30, 10, 50), Imports =c(90,50,25,15,50)) 
df2 = data.frame(Industries = c("c1", "c2", "c3", "c4", "c5"), Exports = c(20, 90, 10, 30, 60), Imports =c(40,30,25,55,40)) 

list(df1, df2) %>% map_df(~ filter(., Industries == "c3")) 
#> Industries Exports Imports 
#> 1   c3  30  25 
#> 2   c3  10  25 
  • list(df1, df2)は、それぞれの
  • map_df反復処理し、リストにあなたのデータフレームを組み合わせ約束しています(可能であれば)結果をデータフレームとして返します。
  • ~ filter(., Industries == "c3")は、各データフレームで実行される関数で、Industries == "c3"の行のみを返します。
0

data.tableを使用できます。 (rbindlistlistにデータセットrbindmgetpastelistにデータセットを置き、「産業」と「キー」を設定し、カラム

library(data.table) 
setkey(rbindlist(mget(paste0("df", 1:2))), "Industries")["c3"] 
# Industries Exports Imports 
#1:   c3  30  25 
#2:   c3  10  25 
関連する問題