My goal is to make a for loop that goes through ID's in a database and gets specific info on those ID's and outputs a vector of that information. However, the information needed is labelled by different numbers.Basically if the 1st column of the data equals either of the 9 numbers, I want the first entry of column two to be printed into a vector. My thought was to create an if else statement, so that the code could simply rule out what numbers column one does not have.
I can try to do one number at a time example, metb4can <- indx[indx[,1]==4584,] start <- head(metb4can[,2],1), but there are some iterations that don't include 4584, but do include 4585.
ageStart = c()
for(i in data) {
x <- dbGetQuery(conn, paste('SELECT * FROM table WHERE Person = ', i, ';'))
info = fromJSON(x$info)
indx <- as.data.frame(info$dx)
inrx <- as.data.frame(info$rx)
metb4can <- indx[indx[,1]==4584,]
metb4can3 <-indx[indx[,1]==4585,]
metb4can4 <- indx[indx[,1]==4586,]
metb4can5 <- indx[indx[,1]==4587,]
metb4can6 <- indx[indx[,1]==4588,]
metb4can7 <- indx[indx[,1]==4589,]
metb4can8 <- indx[indx[,1]==4590,]
metb4can9 <- indx[indx[,1]==4591,]
metb4can10 <- indx[indx[,1]==4592,]
if (length(metb4can) == 0){
print('false')
}else if (length(metb4can) > 0){
start <- head(metb4can[,2],1)
}
if (length(metb4can3) == 0){
print('false')
} else if (length(metb4can3) > 0){
start<- head(metb4can3[,2],1)
}
if (length(metb4can4) == 0){
print('false')
}else if (length(metb4can4) > 0){
start<- head(metb4can4[,2],1)
}
if (length(metb4can5) == 0){
print('false')
} else if (length(metb4can5) > 0){
start <- head(metb4can5[,2],1)
}
if (length(metb4can6) == 0){
print('false')
} else if (length(metb4can6) > 0){
start <- head(metb4can6[,2],1)
}
if (length(metb4can7) == 0){
print('false')
} else if (length(metb4can7) > 0){
start <- head(metb4can7[,2],1)
}
if (length(metb4can8) == 0){
print('false')
} else if (length(metb4can8) > 0){
start <- head(metb4can8[,2],1)
}
if (length(metb4can9) == 0){
print('false')
} else if (length(metb4can9) > 0){
start <- head(metb4can9[,2],1)
}
if (length(metb4can10) == 0){
print('false')
} else if (length(metb4can9) > 0){
start <- head(metb4can10[,2],1)
}
print(start)
ageStart <- c(ageStart, capture.output(start))
}
ageEventStarts <- as.data.frame(ageStart)
The expected output should be a vector without NA values, that which every iteration gives the first entry on column 2, given that column one equals to any of the 9 numbers given.
Aucun commentaire:
Enregistrer un commentaire