I have the following code, which reads through the table's column and if the element contains the correct string, it increments a corresponding value in another vector. Here is the code:
dateArray <- integer(365)
for (i in 189500:207097) {
if (grepl("Jan", csvaryana[i, "Date"], ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)) {
for (j in 1:31) {
if (j < 10) {
if (grepl(paste(sprintf(" 0%d", j), ""), csvaryana[i, "Date"], ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE))
dateArray[j] <- dateArray[j] + 1
}
if (grepl(paste(sprintf(" %d", j), ""), csvaryana[i, "Date"], ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE))
dateArray[j] <- dateArray[j] + 1
}
}
}
dateArray
Note that csvaryana is a table with 207,097 rows. The code is supposed to check all rows, but I cut this down to only about 10,000 rows. It takes a few minutes to run this, and much longer for the full code. How can I do this same thing much more quickly? I have heard that for loops are not very efficient.