Check field type in R - r

I am trying to run a loop in columns and check field type.
for(i in names(SQLQUERYOUTPUT)){
abc<- sapply(SQLQUERYOUTPUT[i], class)
print(abc)
if is.numeric(abc)
{
min(SQLQUERYOUTPUT[i])
}
IF is.character(abc)
max(nchar(SQLQUERYOUTPUT[i]))
}
}
if column is numeric then need lowest value and if its string then value with maximum length.
Its not working. Can anyone please help me on this

Your if-clauses are not correct. The function class will always return a string, e.g. "character" or "numeric". This means your if-clauses have to look like:
if(abc == "numeric"){...}
if(abc == "character"){...}

Related

Length Zero in r

Greetings I am getting an error of
Error in if (nrow(pair) == 0) { :argument is of length zero
I have checked the other answers but do not seem to work on a variable like mine. Please check code below, please assist if you can.
pair<-NULL
if(exists("p.doa.ym")) pair <- rbind(pair, p.doa.ym[,1:2])
if(exists("p.doa.yd")) pair <- rbind(pair, p.doa.yd[,1:2])
if(nrow(pair) == 0) {
print("THERE ARE NO MATCHES FOR TODAY. STOP HERE")
quit()
}
Since you set pair=NULL and then it might happen that pair stays null if those two if statements are not true, you either need to check if pair is null first, or you could set pair to an empty data frame, or something else.
One option:
if (!is.null(pair)) {
if (nrow(pair)==0) {
# your code
}
}
Another option:
pair=data.frame()
# your code

Select any character string over an NA in an If statement in R

I am trying to create a function which will look at two vectors of character labels, and print the appropriate label based on an If statement. I am running into an issue when one of the vectors is populated by NA.
I'll truncate my function:
eventTypepriority=function(a,b) {
if(is.na(a)) {print(b)}
if(is.na(b)) {print(a)}
if(a=="BW"& b=="BW",) {print("BW")}
if(a=="?BW"& b=="BW") {print("?BW")}
...#and so on
}
Some data:
a=c("Pm", "BW", "?BW")
b=c("PmDP","?BW",NA)
c=mapply(eventTypepriority, a,b, USE.NAMES = TRUE)
The function works fine for the first two, selecting the label I've designated in my if statements. However, when it gets to the third pair I receive this error:
Error in if (a == "?BW" & b == "BW") { :
missing value where TRUE/FALSE needed
I'm guessing this is because at that place, b=NA, and this is the first if statement, outside of the 'is.na' statements, that need it to ignore missing values.
Is there a way to handle this? I'd really rather not add conditional statements for every label and NA. I've also tried:
-is.null (same error message)
-Regular Expressions:
if(a==grepl([:print:]) & b==NA) {print(a)}
In various formats, including if(a==grepl(:print:)... No avail. I receive an 'Error: unexpected '[' or whatever character R didn't like first to tell me this is wrong.
All comments and thoughts would be appreciated. ^_^
if all your if conditions are exclusives, just call return() to avoid checking other conditions when one is met:
eventTypepriority=function(a,b) {
if(is.na(a)) {print(b);return()}
if(is.na(b)) {print(a);return()}
if(a=="BW"& b=="BW",) {print("BW");return()}
if(a=="?BW"& b=="BW") {print("?BW");return()}
...#and so on
}
You need to use if .. else statements instead of simply if; otherwise, your function will evaluate the 3rd and 4th lines even when one of the values is n/a.
Given you mapply statement, I also assume you want the function to output the corresponding label, not just print it?
In that case
eventTypepriority<-function(a,b) {
if(is.na(a)) b
else if(is.na(b)) a
else if(a=="BW"& b=="BW") "BW"
else if(a=="?BW"& b=="BW") "?BW"
else "..."
}
a=c("Pm", "BW", "?BW")
b=c("PmDP","?BW",NA)
c=mapply(eventTypepriority, a,b, USE.NAMES = T)
c
returns
Pm BW ?BW
"..." "..." "?BW"
If you actually want to just print the label and have your function return something else, you should be able to figure it out from here.

R: How to log parameter name and value

This is an extension of the previous post In R, how to get an object's name after it is sent to a function? and its also popular duplicate How to convert variable (object) name into String [duplicate]
The idea is that you have a list of parameter values, e.g.
parameters <- c(parameter1,parameter2,...)
and you want to write to a file the parameter name (the variable) and value, e.g
parameter1: 500
parameter2: 1.2
...
In the previous posts we saw this function:
getVariablesName <- function(variable) {
substitutedVariable <- substitute(variable);
if (length(substitutedVariable) == 1) {
deparse(substitutedVariable)
} else {
sub("\\(.", "", substitutedVariable[2])
}
}
which is capable of recalling the passed variable's "name"
getVariablesName(foo)
'foo'
but when a variable is passed twice, we loss this information, e.g.
logThis <- function(thingsToLog, inThisFile) {
for (thing in thingsToLog) {
write(paste(getVariablesName(thing),":\t",thing),
file = inThisFile,
append = TRUE)
}
}
the idea being to pass logThis(c(parameter1,parameter2,...), "/home/file.txt")
So how can we retain the variables' "names" as they get encapsulated in a vector, passed to the function logThis and then looped through?

Error: missing value where True/False

I am trying to delete all values in a list that have the tag ".dsw". My list is a list of files using the function list.files. This is my code:
for (file in GRef) {
if (strsplit(file, "[.]")[[1]][3] == "dsw") {
#GRef=GRef[-file]
for(n in 1:length(GRef)){
if (GRef[n] == file){
GRef=GRef[-n]
}
}
}
}
Where GRef is the list of file names. I get the error listed above, but I dont understand why. I have looked at this post: Error .. missing value where TRUE/FALSE needed, but I dont think it is the same thing.
You shouldn't attempt to to modify a vector while you are looping over it. The problem is your are removing items you are then trying to extract later which is causing the missing values. It's better to identify all the items you want remove first, then remove them. For example
GRef <- c("a.file.dsw", "b.file.txt", "c.file.gif", "d.file.dsw")
exts <- sapply(strsplit(GRef, "[.]"), `[`, 3)
GRef <- GRef[exts!="dsw"]

How to grab a specific column based off of a function input

I have a function that takes as input a specific column name. I then want to grab the column of a data frame that corresponds to that name. The problem is if I have code like this:
New <- function(name) {
dataframe$name
}
then it looks for the column with the name "name" and not the name I input in the function. Is there any way to get around this?
Just to add my 5c. Two alternatives above behave differently when the column with the given name does not exist. This may be important for what you do.
dataframe[, name]
will return an error, while
dataframe[[name]]
will return NULL
As Tyler mentioned in a comment
New <- function(name) { dataframe[, name] }
or, alternatively,
New <- function(name) { dataframe[[name]] }

Resources