-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetData
More file actions
96 lines (75 loc) · 2.35 KB
/
Copy pathgetData
File metadata and controls
96 lines (75 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Usage:
# myData <- getData("pathtofile")
getData <- function(file){
# variables of interest to read in
variables <- c("PMID","DP ","JID ", "MH ")
con <- file(file, "rt")
test <- substr(scan(con, what = "", sep = "\n", quiet = TRUE), 1, 4)
close(con)
varLines <- which(test %in% variables) # identifying variables of interest in file
# saving information from variables of interest
#create a skip vector for lines to skip in next scan
#skip argument in scan skips from the last entry read, hence varlines[i]-varlines[i-1]-1
#if statement ensures blank lines don't get stored as data
skip <- vector()
skip[1] <- 1
for(i in 2:length(varLines)){
skip[i] <- (varLines[i]-varLines[i-1]-1)
}
con <- file(file, "rt")
entry <- list()
for(i in 1:length(varLines)){
entry[i] <- scan(con, sep="\n", what="", quiet=TRUE, skip = skip[i],blank.lines.skip=FALSE,nlines=1)
if(entry[i]==""){
entry[i] <- scan(con,sep="\n",what="",quiet=TRUE,skip=0,nlines=1,blank.lines.skip=FALSE)
}
}
close(con)
# creates list varPlusInfo of each variable name and corresponding information as two vectors inside one list element
varPlusInfo <- list()
for(i in 1:length(entry)){
x <- unlist(entry[i])
varPlusInfo[[i]] <- substr(x,1,4)
varPlusInfo[[i]][2] <- substr(x,7,nchar(x))
}
PMIDindex <- vector()
for(i in 1:length(varPlusInfo)){
if(varPlusInfo[[i]][1] == "PMID"){
PMIDindex <- c(PMIDindex,i)
}
}
splitentry <- function(full,ind){
if(ind==length(PMIDindex)){
temp <- full[PMIDindex[ind]:length(full)]
}
else{
temp <- full[PMIDindex[ind]:(PMIDindex[ind+1]-1)]
}
return(temp)
}
#create empty dataframe
d <- data.frame(PMID=vector(),DP=vector(),JID=vector(),MH=I(list()))
for(i in 1:length(PMIDindex)){
temp <- splitentry(varPlusInfo,i)
MH <- vector()
for(j in 1:length(temp)){
if(temp[[j]][1]=="MH "){MH[j] <- temp[[j]][2]}
}
MH <- MH[!is.na(MH)] #remove NAs
for(k in 1:length(temp)){
if(temp[[k]][1]=="PMID"){
d[i,1] <- temp[[k]][2]
}
if(temp[[k]][1]=="DP "){
d[i,2] <- temp[[k]][2]
}
if(temp[[k]][1]=="JID "){
d[i,3] <- temp[[k]][2]
}
if(temp[[k]][1]=="MH "){
d[[i,4]] <- MH
}
}
}
return(d)
}