1

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
4
  • 1
    So you want to check if a column name belongs to a list? Sorry, finding it hard to translate your question. Commented Dec 30, 2017 at 10:57
  • The same with Sam please can you explain a little more your question? You have sample_list1 and sample_list2 and you want to compare the values between these two lists if they are the same? Commented Dec 30, 2017 at 11:07
  • No, I would like to check which list the user chose, i. e. sample_list1 with A, B, C variables or sample_list2 with E, F, G variables. Then use the selected variables from the chosen list. Commented Dec 30, 2017 at 11:07
  • @Kohmoc If that is the case please check my answer. Commented Dec 30, 2017 at 11:12

2 Answers 2

3

If I've understood your question correctly you want your function to tell you if a given name is contained in either your list 1 or your list 2, or neither.

I've written some code (and tested) so this should do the trick:

sample_list1 <- list(
  varA = 11,
  varB = 22,
  varC = 33)

sample_list2 <- list(
  varE = 44,
  varF = 55,
  varG = 66)

sample_fun <- function(name_list) {
  # Check if the selection is in list1 or list2 
  if (name_list %in% sample_list1) {
    print ("Belongs to list 1") 
  }
  else if (name_list %in% sample_list2) {
    print("Belongs to list 2") 
  }
  else stop ("Incorrect list name.")
}

sample_fun(sample_list1$varA) # Belongs to list 1
sample_fun(sample_list1$varB) # Belongs to list 1
sample_fun(sample_list2$varE) # Belongs to list 2
sample_fun(sample_list2$varF) # Belongs to list 2

Although I'm a little confused by your conditional statements. You check for 2 different possibilities yet output the same result (print(name_list)). How would you determine which condition was satisfied?

I made some minor adjustments to your code.

Sign up to request clarification or add additional context in comments.

2 Comments

That's what I meant and it solves my problem. Don't be confused by my conditional statements because it was just an example.
Great, glad I could help.
0

Just a try from me:

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$varA || name_list == sample_list1$varB || name_list == sample_list1$varC) {
    print("list 1")
  } else {
    print("list 2")
  }
}

sample_fun(sample_list1$varA) # "list 1"
sample_fun(sample_list1$varB) # "list 1"
sample_fun(sample_list2$varE) # "list 2"
sample_fun(sample_list2$varF) # "list 2"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.