12個の異なるtxtファイルをデータフレームに結合すると、次のようになります。
各ファイルには異なる数のデータが含まれており、ファイル名は日付付きの名前でした。 「Student-Score-2010-10.txt」。
各ファイルは1か月を表します。
各行に日付を戻すにはどうすればよいですか?
id dep score
id511 10 34
id512 10 32
id512 10 34
各行に日付を追加する必要があります
id dep score date
id511 10 34 2010-10
id511 10 34 2010-10
id511 10 34 2010-10
id511 10 34 2010-11
id511 10 34 2010-11
id511 10 34 2010-12
id511 10 34 2011-1
日付を作りました。実際のデータではありません
元のデータ
月例報告"
「XXXXX大学」
「+ -------- + ------ + ----- +」
"| id | dep |スコア|
「+ ------- + ----- + ------ +
"| id593 | 2 | 233 |
回答:
回答№1は0あなたが試みることができる:
files <- list.files(pattern="^Student")
files
#[1] "Student-Score-2010-10.txt" "Student-Score-2010-11.txt"
dat<- do.call(rbind,lapply(files, function(x) {
al <- readLines(x)
al2 <- grep("id[0-9]", al, value=TRUE)
al3 <- gsub("^ +| +$", "", gsub("[[:punct:]]+", "", al2))
al4 <-read.table(text=al3, header=FALSE, sep="", stringsAsFactors=FALSE)
colnames(al4) <- c("id", "dep", "scores")
transform(al4, date=gsub("[[:alpha:]]+\-[[:alpha:]]+\-(.*)\.txt",
"\1",x))}))
dat
# id dep scores date
#1 id592 2 235 2010-10
#2 id593 2 233 2010-11