2016-11-22 2 views
0

ありがとうございます!私は数日間これを試してきました、そして、私は固執しています。私はテキストファイル(リストとしてインポート)をループし、テキストファイルからデータフレームを作成しようとしています。リスト内の項目にテキストの曜日がある場合、データフレームは新しい行を開始し、最初の列(V1)に入力されます。残りのコメントを2番目の列(V2)に入れたいので、文字列を連結しなければならない場合があります。私はgrepl()で条件を使用しようとしていますが、私は最初のデータフレームを設定した後にロジックが失われてしまいます。テキストをループしてデータフレームを作成する

ここでは、テキストファイルからFacebookデータであるRに取り込むテキストの例を示します。 []はリスト番号を示します。それは長いファイル(50K +行)ですが、私は日付の列を設定しています。

[1] 15:57 EDT

[2] フットボール時の2016年8月25日(木曜日)!!我々は計画を立てる必要がある!!私は昨年触れられていませんが、私の男に文章をしました。だから私は私の最後に見える!あなたは何を食べていますか?

9:17 EDTで

[3] 2016年8月14日(日曜日)

[4]マイケルはジェイソンの投稿を共有しました。

[5]この鳥は最近、ここで私は読んだことが政治的ポストの大半よりもたくさん賢くある

[6] 2016年8月14日(日曜日)午前8時44分午前EDTで

[7]マイケルとKurtは今の友達です。

最終結果は、曜日がデータフレームで新しい行を開始し、残りのリストがデータフレームの2番目の列に連結されるデータフレームになります。そうエンドデータ名声は(V2にV1 [1]および[2])

行1あろう

行2(V1 [3]及び[4]、[5] V2で)

行3([6] V2にV1及び[7])に

ここでは、私のコードの開始であり、Iは、データフレームの第2列V1を正しく取り込むために得ることはできませんが。

### Read in the text file 
temp <- readLines("C:/Program Files/R/Text Mining/testa.txt") 

### Remove empty lines from the text file 
temp <- temp[temp!=""] 

### Create the temp char file as a list file 
tmp <- as.list(temp) 

### A days vector for searching through the list of days. 
days <- c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday", "Saturday") 
df <- {} 

### Loop through the list 
for (n in 1:length(tmp)){ 

    ### Search to see if there is a day in the list item 
    for(i in 1:length(days)){ 
      if(grepl(days[i], tmp[n])==1){ 
    ### Bind the row to the df if there is a day in the list item 
        df<- rbind(df, tmp[n]) 
      } 
    } 
### I know this is wrong, I am trying to create a vector to concatenate and add to the data frame, but I am struggling here.  
d <- c(d, tmp[n]) 
} 
+0

'dput'を使用してデータを共有してください。 –

答えて

1

ここtidyverseを使用してオプションです:

library(tidyverse) 

text <- "[1] Thursday, August 25, 2016 at 3:57pm EDT 

[2] Football time!! We need to make plans!!!! I texted my guy, though haven't been in touch sense last year. So we'll see on my end!!! What do you have cooking??? 

[3]Sunday, August 14, 2016 at 9:17am EDT 

[4]Michael shared Jason post. 

[5]This bird is a lot smarter than the majority of political posts I have read recently here 

[6]Sunday, August 14, 2016 at 8:44am EDT 

[7]Michael and Kurt are now friends." 

df <- data_frame(lines = read_lines(text)) %>% # read data, set up data.frame 
    filter(lines != '') %>% # filter out empty lines 
    # set grouping by cumulative number of rows with weekdays in them 
    group_by(grp = cumsum(grepl(paste(weekdays(1:7, abbreviate = FALSE), collapse = '|'), lines))) %>% 
    # collapse each group to two columns 
    summarise(V1 = lines[1], V2 = list(lines[-1])) 

df 
## # A tibble: 3 × 3 
##  grp           V1  V2 
## <int>          <chr> <list> 
## 1  1 [1] Thursday, August 25, 2016 at 3:57pm EDT <chr [1]> 
## 2  2 [3]Sunday, August 14, 2016 at 9:17am EDT <chr [2]> 
## 3  3 [6]Sunday, August 14, 2016 at 8:44am EDT <chr [1]> 

このアプローチは、V2のためのリスト列を使用していますおそらくあなたのデータを保存するという点で最良の方法であるが、あなたならばpastetoStringを使用必要。


ほぼ同等のベースR:

df <- data.frame(V2 = readLines(textConnection(text)), stringsAsFactors = FALSE) 

df <- df[df$V2 != '', , drop = FALSE] 

df$grp <- cumsum(grepl(paste(weekdays(1:7, abbreviate = FALSE), collapse = '|'), df$V2)) 

df$V1 <- ave(df$V2, df$grp, FUN = function(x){x[1]}) 

df <- aggregate(V2 ~ grp + V1, df, FUN = function(x){x[-1]}) 

df 
## grp           V1 
## 1 1 [1] Thursday, August 25, 2016 at 3:57pm EDT 
## 2 2 [3]Sunday, August 14, 2016 at 9:17am EDT 
## 3 3 [6]Sunday, August 14, 2016 at 8:44am EDT 
##                                         V2 
## 1 [2] Football time!! We need to make plans!!!! I texted my guy, though haven't been in touch sense last year. So we'll see on my end!!! What do you have cooking??? 
## 2          [4]Michael shared Jason post., [5]This bird is a lot smarter than the majority of political posts I have read recently here 
## 3                                [7]Michael and Kurt are now friends. 
+0

お返事ありがとうございました!私は平日の機能からエラーが発生しています。 grepl(paste(weekdays(1:7、省略形= FALSE)、collapse = "|")):引数 "x"がなく、デフォルトはありません 私は平日を使用しようとすると、同様のエラー、それを今読んで。 –

+0

'paste'コールは' '金曜日|土曜日|日曜日|月曜日|火曜日|水曜日|木曜日' 'grepl'のパターンとして文字列を作成することです。あなたは好きなように文字列を作成することができます。 '平日(Sys.Date()+ 1:7)'はうまくいくはずです。正直なところ、数値を渡すと、どのメソッドが呼び出されているのか少し曖昧です。 'grepl'(行の列)の' x'パラメータもそこにあることを確認してください。同じエラーが発生する可能性があります。 – alistaire

+0

WHOOT !!!!ありがとう、トン!私はRを学んでいるだけですが、このコードは素晴らしいです!私は要約をどのように使用し、第2列をリストに連結したのが好きです。私はtibbles、cumsumを読んで、コードを完全に理解するために要約しています、もう一度ありがとう、これは素晴らしいです!!!! –

関連する問題