How to remove a dataframe in R by a string variable? - r

I have a question.
For example, I would like to remove a dataframe, df_to_remove, in R.
I can remove it in this way: rm("df_to_remove").
If I set a string variable called dataframe_name.
dataframe_name = "df_t_remove"
Why the following commend does not work?
rm(eval(dataframe_name))?
How to remove a dataframe in R by a string variable?

Use the list = argument in rm():
x <- 5
y <- 'x'
rm(list = y)

Related

Changing a variable name within the formula in a loop in R

I want to change the variable (which represents a dataframe column), that is a part of the formula, for looping this formula. It's important that I want to insert one var at the moment, because I want to work with this variable later, and only then to change it to another one (so, I guess "lapply" with a list of variables wouldn't be a solution?)
svychisq(~var1 + strata, svy_design)
I need this var1 (name of column) to be changed in a loop / function
Get all the variables in a vector and create a formula object using sprintf/paste0.
library(survey)
cols <- c('var1', 'var2')
#If you want all the variables that have 'var' in it.
#cols <- grep('var', names(df), value = TRUE)
result <- lapply(cols, function(x) {
svychisq(as.formula(sprintf('~%s + strata', x)), svy_design)
})

For Loop to convert string to list

I have a column in a data frame, which contains string values. I want to convert these values to lists of characters. When i try to execute the following code:
library(tidyverse)
col <- c("a,b,c,d","e,f,h")
df <- data_frame(col)
for (i in 1:length(df$col)) {
df$col[[i]] <- as.vector(unlist(strsplit(df$col[[i]],",")),mode ="list")
}
i get this error message:
Error in df$col[[i]] <- as.vector(unlist(strsplit(df$col[[i]], ",")), : more elements supplied than there are to replace
Traceback:
Is there a way to convert all the values in the column to lists ?
Thanks
If I understand your question correctly, then this will do the trick:
rapply(df, list)

Why do data frame columns in R that I specify as numeric change to character?

I initialize a data frame in R with the following code:
pcts <- data.frame(group=character(),
bonus.type=character(),
success.rate=double(),
stringsAsFactors = FALSE)
I then add rows to the data frame with:
pcts[nrow(pcts)+1,] <- c(paste(varname,gname,sep="="), btype, pct)
However, when I return pcts from the function, the success.rate column is character-valued. To use a value from this column in a mathematical operation, I need to use the as.double or as.numeric function. Any changes I could make to the code so this does not happen?
Try running the right-hand side of your command alone without assigning it to pcts.
This is where the coercion happens -- c creates an atomic vector, which can only have one type (here, everything is forced as character).
You should pass a list instead (the most basic object in R for handling multiple types), or better yet, use rbind:
pcts = rbind(pcts, data.frame(group = paste(varname,gname,sep="="),
bonus.type = btype,
success.rate = pct))

R - cannot use variable generated by for loop as argument in table()

I'm trying to extract prop.test p-values over a set of columns in a dataframe existing in the global environment (df) and save them as a dataframe. I have a criteria column and 19 variable columns (among others)
proportiontest <- function() {
prop_df <- data.frame()
for(i in 1:19) {
x <- paste("df$var_", i, sep="")
y <- (prop.test(table(df$criteria, x), correct=FALSE))$p.value
z <- cbind (x, y)
prop_df <- rbind(prop_df, z)
}
assign("prop_df",prop_df,envir = .GlobalEnv)
}
proportiontest()
When I run this I get the error:
Error in table(df$criteria, x) : all arguments must have the same length
When I manually insert the column name into the function (instead of x) everything runs fine. e.g.
y <- (prop.test(table(df$criteria, df$var_1), correct=FALSE))$p.value
I seem to have the problem of using the variable (x) value generated via the for loop as the argument.
What am I missing or doing wrong in this case? I have tried passing x into the table() function as.String(x) as.character(x) among countless others to no avail. I cannot seem to understand in which form the argument must be. I'm probably misunderstanding something very basic in R but it's driving me insane and I cannot seem to formulate the question in a manner where google/SO can help me.
Currently in your function x is just a string. If you want to use a column from your data frame df you can do this in your for loop:
x <- df[,i]
You'll then need to change z or you'll be cbinding a column to a single p value, maybe just change to this:
z <- cbind(i,y)
so that you know which df column belongs to each p value.
You should be careful as well since the function will search for df created within itself and then move to the parent environment if it doesn't find it, so maybe you could pass the df as an argument to avoid any mistakes.

In R, how do I treat a parameter as a variable, where that variable is the name of its contents? [duplicate]

I have this sample code to create a new data frame 'new_data' from the existing data frame 'my_data'.
new_data = NULL
n = 10 #this number correspond to the number of rows in my_data
conditions = c("Bas_A", "Bas_T", "Oper_A", "Oper_T") # the vector characters correspond to the target column names in my_data
for (cond in conditions){
for (i in 1:n){
new_data <- rbind(new_data, c(cond, my_data$cond[i]))
}
}
The problem is that my_data$cond (where cond is a variable, and not the column name) is not accepted.
How can I call a column of a data frame by using, after the dollar sign, a variable value?
To access a column, use:
my_data[ , cond]
or
my_data[[cond]]
The ith row can be accessed with:
my_data[i, ]
Combine both to obtain the desired value:
my_data[i, cond]
or
my_data[[cond]][i]
I guess you need get().
For example,
get(x,list), where list is the list and x is the variable(can be a string), which equals list$x.
But in get(x,list), x can be a variable while using $, x cannot be a variable.
$ works on columns, not individual column objects. It's a form of vectorization. The code
corrections$BookDate = as.Date(corrections$BookDate, format = "%m/%d/%Y")
converts the contents of the BookDate column of the corrections table from strings to Date objects. It performs it in one operation, assignment.
Do the following and it will fix your problem:
new_data <- rbind(new_data, c(cond, my_data$cond))

Resources