在此先感谢!我一直在尝试这几天,我有点卡住了。我试图循环访问一个文本文件(作为列表导入),并从文本文件创建一个数据框。如果列表中的项目在文本中具有星期几,并且将填充到第一列(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'请分享您的数据。 –