2017-03-01 7 views
0

私は、データフレームは、データと呼ばれています:分割データフレームを動的

**Select.Actions**  **Current.State** **Next.State** 
Hire new staff   Out of Benchmark Withinbenchmark 
Hire new staff   Out of Benchmark Withinbenchmark 
Discuss with Customer Withinbenchmark Withinbenchmark 
Discuss with Customer Withinbenchmark Withinbenchmark 
Discuss with Customer Out of Benchmark Out of Benchmark 
Fire new staff   Out of Benchmark Withinbenchmark 
Discuss with Customer Withinbenchmark Withinbenchmark 
Discuss with Customer Out of Benchmark Withinbenchmark 
Fire new staff   Out of Benchmark Withinbenchmar 

私はSelect.Actionsの値に基づいて、独立したデータフレームを持っていると思います。

#select First Column of dataframe 
d<-data[1] 

次に、データをdの入力と一致させたいとします。 dはダイナミックであり、それは時間の経過とともに変化しますので、私は別のデータフレームにデータフレームを分割するためにループを書いたので:

split<-for(i in 1:length(d)){ 
z[i]<-subset(data, data[,"Select.Actions"] %in% d[i],select=c(Current.State,Next.State))} 

それから私は、次の警告メッセージが表示されました。

Warning message: 
In `[<-.data.frame`(`*tmp*`, i, value = list(Current.State = integer(0), : 
    provided 2 variables to replace 1 variables 

ロジックについてアドバイスしてください。

出力はNULLです。

+0

'?split'関数の使用を中止するにはどうすればよいですか? – discipulus

+0

split関数を使用する場合は、ループを使用する必要があります。これは、Select.Actionsの入力が動的で、ユーザーによって変更されるためです。だから私は、データフレームを分割する動的なコードを記述する必要があります。 – user

+0

'd <-data [1]'は列btwを選択しません。 'data [、1]'が必要です。私はあなたがやっていることをかなり得ることはありません。 'd'はどのような値を取ることができますか? – Jean

答えて

1

z[i]<-subset(data, ...に複数の行と列を割り当てている場合は、rbindを使用できます。私は、subsetをHadelyによって説明されているように使用しないことをお勧めします。heredplyr解決策があなたに役立つかどうかお知らせください。

library(dplyr) 
data <- read.table(text = 'Select.Actions,Current.State,Next.State 
Hire new staff,Out of Benchmark,Withinbenchmark 
Hire new staff,Out of Benchmark,Withinbenchmark 
Discuss with Customer,Withinbenchmark,Withinbenchmark 
Discuss with Customer,Withinbenchmark,Withinbenchmark 
Discuss with Customer,Out of Benchmark,Out of Benchmark 
Fire new staff,Out of Benchmark,Withinbenchmark 
Discuss with Customer,Withinbenchmark,Withinbenchmark 
Discuss with Customer,Out of Benchmark,Withinbenchmark 
Fire new staff, Out of Benchmark,Withinbenchmar', 
        header = TRUE, sep =",", stringsAsFactors = FALSE) 



z <- NULL 
for(i in 1:nrow(data)) 
{ 
    interm_data <- data %>% filter(Select.Actions == data[i,1]) %>% select(Current.State, Next.State) 
    if(is.null(z)) 
    { 
    z<- interm_data 
    }else{ 
    z<- rbind(z,interm_data) 
    } 
    print(data[i,1]) 
    print(interm_data) 

} 

**更新**

ユーザーのコメントに基づいて。

z <- list() 
trim <- function (x) gsub("^\\s+|\\s+$", "", x) 
for(i in unique(data$Select.Actions)) 
{ 
    z[[trim(i)]] <- data %>% filter(Select.Actions == i) %>% select(Current.State, Next.State) 
} 
list2env(z ,.GlobalEnv) 
# Now you will have 3 data sets `Hire new staff`, `Fire new staff` and `Discuss with customer` in your workspace. 

しかし、私はあなたのニーズに合わせて最初のループを使用しません。

+0

ありがとうございます、しかし出力に重複があります。 [1]新しいスタッフの雇用[2]顧客との話し合い[3]新しいスタッフの募集さまざまなアクションに基づいてデータフレームをフィルタリングし、各データフレームを新しいものとして保存したい – user

+0

nrow(データ) – user

+0

新しい更新コードはどうやって複製を削除し、3つのデータフレームを別々に作成するのですか? – discipulus

関連する問題