I need to detect if my first row of observations is a row of names. Always when I import the data are imported as character columns from the spreasheet (readxl package).
By the structure of the data, a non-name row, always contain at least 8 numeric values.
rowNoName <- c("23-234", "Bank of Wisdom", 1:8)
rowName <- c("code of acc", "name of acc", "ac", "li", "ui", "op", "o", "p", " e", "i")
So, in this logic, I use the implicit coercion to do my task. From a character element that is originally a numeric class element, the coercion is simple. But from an element that is originally a text string, the implicit coercion fails and throw a NA. The rule is:
testName <- function(row) {
if (sum(!is.na(as.numeric(row))) >= 8) {
print("row without names")
} else {
print("row with names")
}
This function solve the problem but exist another more formal way to do this? I mean, to avoid the warning message of the coercion in the output.
> testName(row)
[1] "row with names"
Warning message:
In testName(row) : NAs introduced by coercion