1 ## File : 03_compute-sum-of-classes_2010.r
2 ## Description : Calculer les synchrones pour chaque groupe obtenu par le
7 setwd("~/Documents/projects/2014_EDF-Orsay-Lyon2/codes/")
9 ## 1. Read auxiliar data files ####
11 identifiants <- read.table("identifs.txt")[ ,1]
12 dates0 <- read.table("datesall.txt")[, 1]
13 dates <- dates0[grep("2010", dates0)]
16 n <- length(identifiants)
19 blocks <- c(rep(6500, 3), 5511)
21 # Fit of the clustering : clfit
22 #load('~/Documents/projects/2014_EDF-Orsay-Lyon2/res/clfit500.Rdata')
23 load('~/Documents/projects/2014_EDF-Orsay-Lyon2/res/clfit200.Rdata')
24 load('~/Documents/projects/2014_EDF-Orsay-Lyon2/res/clfit200RC.Rdata')
25 # table(clfit$clustering)
27 dfclust <- data.frame(cluster = clfit$clustering)
28 # read write.table(dfclust, file = "../res/clfit200RC-random.txt") for random
30 K <- nrow(clfit$clusinfo)
31 #dfclust <- head(dfclust, 50) # just for testing purpouses
32 synchros <- matrix(0, ncol = p, nrow = K)
35 ## 2. Process the large file ####
38 con <- file("~/tmp/2010_full.txt") # Establish a connection to the file
39 open(con, "r") # Open the connection
41 for(b in seq_along(blocks)){ # Reading loop
43 actual <- readLines(con = con, n = nb )
44 auxmat <- matrix(unlist(strsplit(actual, " ")), ncol = p + 1, byrow = TRUE)
47 datamat <- t(apply(auxmat[, -1], 1, as.numeric))
48 rownames(datamat) <- substr(auxmat[, 1], 2, 7)
51 # obtain for each line of datamat the cluster membership (if any)
52 clustfactor <- dfclust$cluster[match(rownames(datamat), rownames(dfclust))]
55 clustk <- which(clustfactor == k)
56 if(length(clustk) > 0) {
58 if(length(clustk) > 1) {
59 synchrosk <- colSums(datamat[which(clustfactor == k), ])
61 synchrosk <- datamat[which(clustfactor == k), ]
63 synchros[k, ] <- synchros[k, ] + synchrosk
70 close(con) # close connection to the file
72 synchros <- data.frame(t(synchros), total = colSums(synchros))
74 # write.table(synchros, file = "~/tmp/2010_synchros200RC.txt")
75 # write.table(synchros, file = "~/tmp/2010_synchros200-random.txt")
80 dfclust <- read.table("clfit200muchos.txt")
83 synchros <- matrix(0, ncol = p, nrow = K)
84 clustfactor <- dfclust[match(rownames(datamat), rownames(tdata)), pepe]
86 clustk <- which(clustfactor == k)
87 if(length(clustk) > 0) {
88 if(length(clustk) > 1) {
89 synchrosk <- colSums(datamat[which(clustfactor == k), ])
91 synchrosk <- datamat[which(clustfactor == k), ]
93 synchros[k, ] <- synchros[k, ] + synchrosk
98 synchros <- data.frame(t(synchros), total = colSums(synchros))
99 write.table(synchros, file = paste0(colnames(dfclust)[pepe], "2010.txt"))