I have two lists with different variables and their values. How can I check if the list name is correct in the conditional statement and then use selected element of list in the further part of the algorithm?
sample_list1 <- list(
varA = 11,
varB = 22,
varC = 33)
sample_list2 <- list(
varE = 44,
varF = 55,
varG = 66)
sample_fun <- function(name_list) {
if (name_list == sample_list1) {
print(name_list)
}
else if (name_list == sample_list2) {
print(name_list)
}
else stop ("Incorrect list name.")
}
sample_fun(sample_list1$varA) # It works
sample_fun(sample_list1$varB) # It doesn't work
sample_fun(sample_list2$varE) # It works
sample_fun(sample_list2$varF) # It doesn't work