在此先感謝!我一直在嘗試這幾天,我有點卡住了。我試圖循環訪問一個文本文件(作爲列表導入),並從文本文件創建一個數據框。如果列表中的項目在文本中具有星期幾,並且將填充到第一列(V1)中,則數據框將開始一個新行。我想將其餘的評論放在第二列(V2)中,我可能必須將字符串連接在一起。我試圖用grepl()來使用條件語句,但是在設置初始數據框後,我對邏輯有些迷失。通過文本循環創建數據幀
這裏是我使成R的示例文本(這是數據的Facebook從文本文件)。 []表示列表號。這是一個很長的文件(50K +行),但我有日期列設置。
[1] 星期四8月25日,2016年下午3點57分EDT
[2] 足球時間!我們需要制定計劃!我發短信給我的傢伙,雖然去年沒有接觸過。所以我們會看到我的結局!你有什麼烹飪?
[3]週日,2016年8月14日在9:17 EDT
[4]邁克爾·傑森共享後。
[5]這隻鳥是比大多數政治職位的我看了最近這裏
[6]週日,2016年8月14日在上午08時44 EDT
[7]邁克爾聰明很多和庫爾特現在是朋友。在一週的某一天在數據幀開始一個新行,而列表的其餘部分被連接成數據幀的第二列
的最終結果將是數據幀。因此最終數據名聲將是
行1([1]在V1和[2]在V2)
行2([3]在V1和[4],[5]在V2)
行3([6]在V1和[7]在V2)
這裏是我的代碼開始,我可以得到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])
}
使用'dput'請分享您的數據。 –