odbcFetchRows(channel, max = max, buffsize = buffsize, nullstring = nullstring, : negative length vectors are not allowed - odbc

SS of the error
I encountered it while submitted a coursera assignment
I have tried many solution like
Finding Missing argument in the sqlFetch Function
Re-estabhlishing the connection
But none have worked so far any suggestion then please leave a comment
Thanks!!!!

Related

Error - 'x' and 'y' must have the same length

I am very new to coding and working on a Homework assignment for class.
This is the code and error I am getting:
Code:
cor.test(HR_comma_sep$average_monthly_hours , HR_comma_sep$number_project , conf.level = .99)
Error:
Error in cor.test.default(HR_comma_sep$average_monthly_hours, HR_comma_sep$number_project, : 'x' and 'y' must have the same length
Any help would be appreciated...keep in mind I am new to this so an answer in layman's terms would help. If you speak code to me I probably won't understand. :)
Thank you in advance for your help.

R package 'checkmate': testString is successful for vector but not assertString, why?

I am currently reading the documentation for checkmate package and I find an issue when dealing with a vector of strings.
testString(letters)
#FALSE
assertString(letters)
#Error in assertString(letters) :
#Assertion on 'letters' failed: Must have length 1.
It seems that assertString does not support test on vectors. Why is that? Any idea is welcome.
The documentation in assertString states that "a string is defined as a character scalar." The function you are seeking is assertCharacter.
assertString is just a shortcut for assettCharacter (x, len =1)

How do I convert for loop argument from Matlab to R?

I have the following for loop argument in Matlab (not my code) :
for phi=2*pi:-2*pi/RES:0
where
RES = 360; % angular resolution
I am having trouble to think of a valid conversion to R code. Can you help me out in any way? Thank you !
EDIT: I was referring to the two intervals in the argument. I get the error message "longer object length is not a multiple of shorter object length"
You have several options:
1)
for(phi in seq(from=2*pi, to=0, by=-2*pi/RES)){
# elaboration here...
}
2)
for(i in RES:0){
phi=i*2*pi/RES
# elaboration here...
}
3)
for(phi in (RES:0)*(2*pi/RES) ){
# elaboration here...
}
Personally, I prefer the second option since it's more readable (you immediately understand that you're looping RES+1 times).

R - Assigning "NA" to objects 'not found' inside a function; is it possible?

I am running a data set (in the example, "data object ") through several different functions in R and concatenating the numeric results at the end. See:
a<-median((function1(x=1,dataobject,reps=500)),na.rm=TRUE)
b<-median((function2(x=1,dataobject,reps=500)),na.rm=TRUE)
c<-median((function3(x=1,dataobject,reps=500)),na.rm=TRUE)
d<-median((function4(x=1,dataobject,reps=500)),na.rm=TRUE)
e<-median((function5(x=1,dataobject,reps=500)),na.rm=TRUE)
f<-median((function6(x=1,dataobject,reps=500)),na.rm=TRUE)
c(a,b,c,d,e,f)
However, some of the functions cannot be run with the data set I am using, and so they return an error; e.g. "function3" can't be run so when it gets to the concatenation step it gives "Error: object 'e' not found" and does not return anything. Is there any way to tell R at the concatenation step to assign a value of "NA" to an object that is not found and continue to run the rest of the code instead of stopping? So that the return would be
[1] 99.233 75.435 77.782 92.013 NA 97.558
A simple question, but I could not find any other instances of it being asked. I originally tried to set up a function to run everything and output the concatenated results, but ran into the same problem (when a function can't be run, the entire wrapper function stops as well and I don't know how to tell R to skip something it can't compute).
Any thoughts are greatly appreciated! Thanks!
A couple of solutions I can think of,
Initialize all the variables you plan to use, so they have a default value that you want.
a = b = c = d = e = NA
then run your code. If an error pops up, you will have NA in the variable.
Use "tryCatch". If you are unaware what this is, I recommend reading on it. It lets you handle errors.
Here is an example from your code,
tryCatch({
a<-median((function1(x=1,dataobject,reps=500)),na.rm=TRUE)
},
error = function(err){
print("Error in evaluating a. Initializing it to NA")
a <<- NA
})

the difference between = and <- operator in the function system.time()

I am using the function system.time() and I have discovered something which surprises me. I often use the allocation symbol “=” instead of “<-”. I am aware most R users use “<-” but I consider “=” clearer in my codes. Thus, I used “=” to allocate a value in a function system.line() and the following error message appeared : Error: unexpected '=' in "system.time(a[,1] ="
Here is the code :
a = matrix(1, nrow = 10000)
require(stats)
system.time(a[,1] = a[,1]*2) #this line doesn't work
#Error: unexpected '=' in "system.time(a[,1] ="
system.time(a[,1] = a[,1]*2) #this line works
system.time(for(i in 1:100){a[,1] = a[,1]*i}) #this line works!!!!
I found : Is there a technical difference between "=" and "<-" which explains that I can’t use “=” in a function to allocate since it is the symbol to assign argument in a function. But I have been surprised to see that it can work sometimes (see following code).
Does anyone know why it works here? (also why it doesn't work in the first case since I guess, a[,1] is not a parameter of the function system.time()...)
Thank you very much.
Edwin.
Wrap your code in { ... } braces and it will work:
system.time({a[,1] = a[,1]*2})
user system elapsed
0 0 0
From ?"<-"
The operators <- and = assign into the environment in which they are
evaluated. The operator <- can be used anywhere, whereas the operator
= is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a
braced list of expressions.
In system.time(a[,1] = a[,1]*2) the equals sign does not mean assignment, it is interpreted as an attempt to bind a "named argument"; but system.time does not have an argument of that name.
In system.time(for(i in 1:100){a[,1] = a[,1]*i}) the equals sign really is doing an assignment; and that works fine.
If you wrote system.time(a[,1] <- a[,1]*2) the <- can only mean assignment, not argument binding, and it works!
But beware! If you wrote system.time(a[,1] < - a[,1]*2), it also "works" but probably doesn't do what you meant!

Resources