Insert variable name into a function in R - r

I'm trying to construct a function that calculate some variances using the survey package. The problem is that I need to insert the name of the variable (not the values of the variables) into a specific function (svyby)
Is something like this:
myfun=function(variable) {
svyby(~variable,~subpop,design,svymean)
}
myfun(P16)
It gives me error. I also tried with
*base[,variable]*
instead of
*variable*
the problem here that base[,variable] gives me the vector with the values of the variable, but I need the name of the variable to be read in the design object. What I mean is, I need that the function insert the name like this
svyby(~P16,~subpop,design,svymean)
I will appreciate any help, thank you in advance,
Gonzalo

Looks like it needs a formula. You can paste a "~" to a string and use as.formula, like this:
myfun = function(variable) {
svyby(as.formula(paste("~", variable)),
~subpop, design, svymean)
}
And then call is like this: myfun("P16"). Note that you will need to use a quoted column name because you are treating it like a string.
Alternatively, you could have your function take a formula:
myfun2 = function(formula) {
svyby(formula,
~subpop, design, svymean)
}
And call it like this: myfun2(~P16).

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...

R - How to remove NAs when using ASSIGN with a FOR LOOP?

I have some code which imports a data frame (keywordsDF), and whilst using a FOR LOOP, it (by using the colnames), creates new variables depending on how many columns there are in it:
keywordsDF = read_excel("//Users//n//Desktop//Keywords.xlsx")
keywordList = colnames(keywordsDF)
for (i in seq_along(keywordList)) {
assign(keywordList[i], keywordsDF[keywordList[i]])
}
This all works fine. However, as the columns are of a different length, it imports NAs into the data.
I would normally remove those NAs for each column like:
consumption = keywordsDF$Consumption[!is.na(keywordsDF$Consumption)]
But I am not sure how to do it in the FOR LOOP (where I don't know the column names). I.e. where do I put the !is.na? As nothing seems to work for me.
You can use keywordsDF[[keywordList[i]]] any place you would use keywordsDF$Consumption, so
# spaced to make the substitution line up
consumption = keywordsDF$Consumption [!is.na( keywordsDF$Consumption )]
assign(keywordList[i], keywordsDF[[keywordList[i]]][!is.na( keywordsDF[[keywordList[i]]] )]
But I'd encourage you not to use assign like this... a list might be nicer like kw_no_na = lapply(keywordsDF[keywordList], na.omit), then use kw_no_na$consumption or kw_no_na[["consumption"]].
You could simply use na.omit() ?
for (i in seq_along(keywordList)) {
assign(keywordList[i], na.omit(keywordsDF[keywordList[i]]))
}

How to use a list name as character

I would like to train a model and give it a name. I would like to use this name as character as well to create a text file with model summary. So I created a function as below
C50Training<-function(ModeName,DF_Trai,Form,
Str_PathSum){
library(C50);
ModeName<-C5.0(formula=Form,data=DF_Trai);
capture.output(summary(ModeName),file=paste(Str_PathSum,"/Summ",ModeName,".txt",sep=""));
}
In the funtion I want to use ModeName as characters. I tried to run it but it does not work. ModelName is a list in this case. How can I use ModelName as character?
To change a variable name to string, you can use deparse and substitute, as follows:
deparse(substitute(ModeName))
It return "ModeName" that can be part of your file path.
I tried this. It works.
ModeName=c(1,2,3)
f<-function(ModeName){
print(paste("/Summ",deparse(substitute(ModeName)),".txt",sep=""))
}
f(ModeName)
and this works too:
ModeName=c(1,2,3)
f<-function(list){
print(paste("/Summ",deparse(substitute(list)),".txt",sep=""))
}
f(ModeName)

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])])))
}

How to use function with variable in R?

in R,the pdf function can save graph in c:/test:
pdf("c:/test")
I want to make a variable substitue pdf ,how can i make it run ?
str<-"pdf"
str("c:/test")
get() does this:
get(str)("c:/test")
s = "pdf" ; do.call(s, list("c:/test"))
or in two steps,
cl <- call(s, "c:/test")
eval(cl)
You can extract the function specified by the name in str with match.fun:
match.fun(str)("c:/test")
By the way: It is not a good idea to name an object str since this is the name of a basic function in R.

Resources