Created
January 26, 2012 10:02
-
-
Save wactbprot/1682043 to your computer and use it in GitHub Desktop.
low level readLines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
path <- "../data/" | |
setwd(path) | |
ff <- list.files(pattern="^.*\\.txt$") | |
dat <- list() | |
l <- 1 | |
for(i in 1:length(ff)){ | |
datName <- ff[i] | |
a <- readLines(datName) | |
head <- unlist(strsplit(a[1], "\t")) | |
for(j in 2:length(a)){ | |
zeile <- unlist(strsplit(a[j], "\t")) | |
dat[["noOfCols"]][l] <- length(zeile) | |
for(k in 1:length(head)){ | |
dat[[head[k]]][l] <- zeile[k] | |
} | |
l <- l+1 | |
} | |
} | |
## > length(dat$C1) # 4 files: | |
## [1] 2000 | |
## Klammern in C1 zählen | |
## number of Adr. | |
infC1 <- list() | |
datC1 <- dat$C1 | |
i <- which(datC1 == "") | |
## L1? | |
for(i in 1:length(datC1)){ | |
sepT <- strsplit(datC1[i] , "") | |
res <- unlist( gregexpr("\\[|\\]", datC1[i])) | |
lres <- length(res) | |
if(lres %% 2 == 0){ | |
noOfAdr <- lres/2 | |
}else{ | |
if(lres == 1 ){ | |
if( res == -1){ | |
noOfAdr <- 1 | |
}else{ | |
noOfAdr <- 0 | |
} | |
} | |
} | |
infC1[["noOfAdr"]][i] <- noOfAdr | |
} | |
## Bsp.: | |
## Verteilung der Anzahl der Adressen: | |
hist(infC1$noOfAdr, labels=TRUE) | |
## ein paar mögliche Ansätze, Test ...: | |
which(infC1$noOfAdr == 0) # !! | |
## integer(0) <---- das ist gut, d.h. nirgends gibt es | |
## [[ oder ]] Klammern also immer paarweise vorhanden wenn vorhanden | |
## | |
## liefert die Positionen der eckigen Klammern | |
gregexpr("*\\[(.*?)\\]", dat$C1[930]) | |
## ^^^^^^^ sowas meite ich bei unserem Treffen | |
## Stichwort reguläre Ausdrücke, regular expressions also regexpr | |
## g steht für global (glaub' ich) | |
## | |
## cool ist auch die country list: | |
a <- read.csv("countrylist.csv", as.is=TRUE) | |
#> a$Common.Name[1] | |
#[1] "Afghanistan" | |
## | |
spd <- unlist(strsplit(dat$C1[930], "[[:blank:][:punct:]]")) | |
## | |
which( spd %in% a$Common.Name ) | |
## [1] 155 182 196 202 239 291 341 373 | |
## 394 412 461 475 524 528 549 567 596 605 613 | |
##[20] 635 657 709 730 753 | |
##> spd[155] | |
##[1] "Peru" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment