R: How to log parameter name and value - r

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?

Related

How can I use a string input from an R function to name a dataset?

I want to create a very simple function that takes part of a large dataset (df) and creates a new dataset in the global environment with a specified name. The problem is that it seems to name the new dataframe "x" instead of the actual string input. Example:
create_dataset<-function(x,rows,columns) {
name<<-df[rows,columns]
}
create_dataset(x="skildpadde",
rows=690:692,
columns=2:7)
How can I use the input "x" as the dataset name?
Use get():
create_dataset<-function(x,rows,columns) {
get(x)[rows,columns]
}
Or, if you trying to assign to x in the global environment:
create_dataset<-function(x,rows,columns) {
assign(x, df[rows,columns],envir = .GlobalEnv)
}
I'm not sure I understand the use case or rationale behind either of these...

How to extract a string value from an array using scripted field in kibana?

Is there a way to extract a string value from an array with the use of if statement in scripted field in kibana. I tried the below code, however, I am unable to filter out the correct and incorrect values in discover tab of kibana. This might be due to remark field is an array.
def result_string = "";
if (doc['nac.keyword'].value =="existing_intent" &&doc['remark.keyword'].value != "acceptable") {
result_string = "incorrect";
}
if (doc['nac.keyword'].value =="existing_intent" &&doc['remark.keyword'].value == "acceptable") {
result_string = "correct";
}
return result_string;`
You can use the contains method defined on Array to check for element membership:
!doc['remark.keyword'].value.contains("acceptable") //does not contain
For this, you might want to ensure first that doc['remark.keyword'].value is indeed an Array.

Using a function to change a variable in R

I am trying to change a variable in a function but even tho the function is producing the right values, when I go to use them in the next sections, R is still using the initial values.
I created a function to update my variables NetN and NetC:
Reproduction=function(NetN,NetC,cnrep=20){
if(NetC/NetN<=cnrep) {
DeltaC=NetC*p;
DeltaN=DeltaC/cnrep;
Crep=Crep+DeltaC;
Nrep=Nrep+DeltaN;
Brep=(Nrep*14+Crep*12)*2/1e6;
NetN=NetN-DeltaN; #/* Update N, C values */
NetC=NetC*(1-p)
print ("'Using C to allocate'")
}
else {
print("Using N to allocate");
DeltaN=NetN*p;
DeltaC=DeltaN*cnrep;
Nrep=Nrep+DeltaN;
Crep=Crep+DeltaC;
Brep=(Nrep*14+Crep*12)*2/1e6;
NetN=NetN*(1-p);
NetC=NetC-DeltaC;
} } return(c(NetC=NetC,NetN=NetN,NewB=NewB,Crep=Crep,Nrep=Nrep,Brep=Brep))}
When I use my function by say doing:
Reproduction(NetN=1.07149,NetC=0.0922349,cnrep=20)
I get the desired result printed out which includes:
NetC=7.378792e-02
However, when I go to use NetC in the next section of my code, R is still using NetC=0.0922349.
Can I make R update NetC without having to define a new variable?
In R, in general, functions shouldn't change things outside of the function. It's possible to do so using <<- or assign(), but this generally makes your function inflexible and very surprising.
Instead, functions should return values (which yours does nicely), and if you want to keep those values, you explicitly use <- or = to assign them to objects outside of the function. They way your function is built now, you can do that like this:
updates = Reproduction(NetN = 1.07149, NetC = 0.0922349, cnrep = 20)
NetC = updates["NetC"]
This way, you (a) still have all the other results of the function stored in updates, (b) if you wanted to run Reproduction() with a different set of inputs and compare the results, you can do that. (If NetC updated automatically, you could never see two different values), (c) You can potentially change variable names and still use the same function, (d) You can run the function to experiment/see what happens without saving/updating the values.
If you generally want to keep NetN, NetC, and cnrep in sync, I would recommend keeping them together in a named vector or list, and rewriting your function to take that list as input and return that list as output. Something like this:
params = list(NetN = 1.07149, NetC = 0.0922349, cnrep = 20)
Reproduction=function(param_list){
NetN = param_list$NetN
NetC = param_list$NetC
cnrep = param_list$cnrep
if(NetC/NetN <= cnrep) {
DeltaC=NetC*p;
DeltaN=DeltaC/cnrep;
Crep=Crep+DeltaC;
Nrep=Nrep+DeltaN;
Brep=(Nrep*14+Crep*12)*2/1e6;
NetN=NetN-DeltaN; #/* Update N, C values */
NetC=NetC*(1-p)
print ("'Using C to allocate'")
}
else {
print("Using N to allocate");
DeltaN=NetN*p;
DeltaC=DeltaN*cnrep;
Nrep=Nrep+DeltaN;
Crep=Crep+DeltaC;
Brep=(Nrep*14+Crep*12)*2/1e6;
NetN=NetN*(1-p);
NetC=NetC-DeltaC;
}
## Removed extra } and ) ??
return(list(NetC=NetC, NetN=NetN, NewB=NewB, Crep=Crep, Nrep=Nrep, Brep=Brep))
}
This way, you can use the single line params <- Reproduction(params) to update everything in your list. You can access individual items in the list with either params$Netc or params[["NetC"]].

Constructing a table for each variable to show time periods in which NA's occur

I would like to create a table in R for each variable in my data set macro to give me the year(s) (we have a date variable timestamp) in which our NA's in the variables occur.
I tried this:
for (var in names(macro)) {
var <- paste("macro$",var, sep="")
print(var)
print(table(year(macro$timestamp[is.na(var)])))
}
but it does not work.
When I don't write it within the for loop it, however, works, e.g.:
table(year(macro$timestamp[is.na(macro$gdp)]))
Does anybody know what I am doing wrong?
You are defining var as a character string, so it is not NA. Try the following...
for (var in names(macro)) {
print(var)
print(table(year(macro$timestamp[is.na(macro[,var])])))
}

Check field type in 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"){...}

Resources