0

ほぼ 3,000 個の csv ファイルをマージし、重複する行を削除して、新しい csv データファイルを作成しています。この目的のために、次のコードを使用しました。

#Grab our list of filenames
filenames <- list.files(path = ".", pattern='^.*\\.csv$')
#Read.csv function
my.read.csv <- function(fnam) { read.csv(fnam, header=FALSE, skip=1, sep=';', col.names=c('ID','tweet','author','local.time','extra'), colClasses=rep('character', 5))}
#Read all the files into one data.frame
my.df <- do.call("rbind", lapply(filenames, my.read.csv)) length(my.df[,1])
#Remove the duplicate tweets
my.new.df <- my.df[!duplicated(paste(my.df$tweet, my.df$author)),]
length(my.new.df[,1])
#Write new dataframe as a .csv file
write.csv(my.new.df, file =paste("Dataset", ".csv"))

関数は想定どおりに機能しますが、出力ファイルは乱雑です。元の csv ファイルはすべて次の構造になっています。

tweet                                                         author    local.time
2012-06-05 00:01:45 @A (A1):  Cruijff z'n (...)#bureausport.  A (A1)    05-06-12 00:01
2012-06-05 00:01:41 @B (B1):  Welterusten #BureauSport        B (B1)    05-06-12 00:01
2012-06-05 00:01:38 @C (C1):  Echt (...) #bureausport         C (C1)    05-06-12 00:01
2012-06-05 00:01:38 @D (D1):  LOL. #bureausport               D (D1)    05-06-12 00:01

ただし、出力ファイルの構造は次のとおりです。

,"ID","tweet","author","local.time","extra"
1,"2012-06-05 00:01:45 @A (A1):  Cruijff z'n (...)#bureausport.","@A (A1)","05-06-12 00:01"
2,"2012-06-05 00:01:41 @B (B1):  Welterusten #BureauSport","@B (B1)","05-06-12 00:01"
3,"2012-06-05 00:01:38 @C (C1):  Echt (...) #bureausport","Aliceislovely (Alice Luyben)","05-06-12 00:01"
4,"2012-06-05 00:01:38 @D (D1):  LOL. #bureausport","@D (D1)","05-06-12 00:01"

したがって、データは列ではなく文字列として表示されます。出力ファイルが元の (入力) csv ファイルと同じ列構造になるように、コード (上記) を調整するのを手伝ってくれることを願っています。

ところで、次のコードを使用して csv ファイルを作成しました。

library(XML)   # htmlTreeParse

twitter.search <- "Keyword"

QUERY <- URLencode(twitter.search)

# Set time loop (in seconds)
d_time = 300
number_of_times = 3000

for(i in 1:number_of_times){

tweets <- NULL
tweet.count <- 0
page <- 1
read.more <- TRUE

while (read.more)
{
# construct Twitter search URL
URL <- paste('http://search.twitter.com/search.atom?q=',QUERY,'&rpp=100&page=', page, sep='')
# fetch remote URL and parse
XML <- htmlTreeParse(URL, useInternal=TRUE, error = function(...){})

# Extract list of "entry" nodes
entry     <- getNodeSet(XML, "//entry")

read.more <- (length(entry) > 0)
if (read.more)
{
for (i in 1:length(entry))
{
subdoc     <- xmlDoc(entry[[i]])   # put entry in separate object to manipulate

published  <- unlist(xpathApply(subdoc, "//published", xmlValue))

published  <- gsub("Z"," ", gsub("T"," ",published) )

# Convert from GMT to central time
time.gmt   <- as.POSIXct(published,"GMT")
local.time <- format(time.gmt, tz="Europe/Amsterdam")

title  <- unlist(xpathApply(subdoc, "//title", xmlValue))

author <- unlist(xpathApply(subdoc, "//author/name",  xmlValue))

tweet  <-  paste(local.time, " @", author, ":  ", title, sep="")

entry.frame <- data.frame(tweet, author, local.time, stringsAsFactors=FALSE)
tweet.count <- tweet.count + 1
rownames(entry.frame) <- tweet.count
tweets <- rbind(tweets, entry.frame)
}
page <- page + 1
read.more <- (page <= 15)   # Seems to be 15 page limit
}
}

names(tweets)

# top 15 tweeters
#sort(table(tweets$author),decreasing=TRUE)[1:15]

write.table(tweets, file=paste("Twitts - ", format(Sys.time(), "%a %b %d %H_%M_%S %Y"), ".csv"), sep = ";")

Sys.sleep(d_time)

} # end if
4

1 に答える 1

2

出力にタブ区切りのフィールドがあり、各列の値を引用符で囲まないようにしたいようです。これを行う方法は次のとおりです。

write.table(mtcars, "mtcars.txt", quote=FALSE, sep="\t")

と上記のコードの呼び出しの出力をすばやくプレビューして比較するにはwrite.csv()、独自のデータで次のようなことを試してください。

write.csv(head(mtcars))
write.table(head(mtcars), quote=FALSE, sep="\t")

編集: (タブ区切りフィールドの代わりに) 各列のデータを完全に水平方向に配置する必要がある場合は、ここに示すwrite.fwfように、パッケージ内gdataを見てください。(「fwf」は「固定幅フォーマット」の略です。)

于 2012-06-15T17:54:49.580 に答える