-1

while (Data$City!="Mumbai" || Data$City!="Delhi" || Data$City!= "Bengaluru")

The error is following :

In while (Data$City!="Mumbai" || Data$City!=...: the condition has length >1 and only the first element will be used.

I want to compare elements of a column with certain values/elements of a vector in while loop and conditionally execute 'n' statements under it? What's the alternative for the limitation above ? What's the alternative : A function/function with apply() or ifelse ?

DataO <- c("Mumbai","Jaipur","Delhi","Chennai","Bengaluru")

Data1 <- setNames(data.frame(matrix(ncol = 1, nrow = 5), c("City"))

for(i in seq_along(DataO))
{
while (DataO!="Mumbai" || DataO!="Delhi" || DataO!= "Bengaluru")
{
Data1$City[i] <- as.character(DataO[i])
}
}

I want to execute the statement under 'while()' when Mumbai==Mumbai(i=1) and then for Delhi==Delhi(i=3) and then for Bengaluru==Bengaluru(i=5). It should skip iteration i=2 and i=4.

Here only the first element(i=1) gets evaluated and added(Mumbai)

> Data1
   City
1 Mumbai
2   <NA>
3   <NA>
4   <NA>
5   <NA>

The desired output :

> Data1
  City
1 Mumbai
2   <NA>
3 Delhi
4   <NA>
5 Bengaluru

The crux here is ' while something(element/row obs) in one place(data column/vector) matches something(element/ row obs) in other place(data column/vector) execute statements till the condition is satisfied and iterate this for all subsequent matches (and break out of the loop) '.

Digression : Can rownames be empty(character type "") in R / Is it possible to assign empty rownames(character type "") in R ?

KrishnAB
  • 25
  • 2
  • Hi! Welcome to Stack Overflow! Please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) in your question. :) – J.Con Jul 02 '18 at 06:13
  • I have edited the code – KrishnAB Jul 02 '18 at 07:21
  • Possible duplicate of [the condition has length > 1 and only the first element will be used in if else statement](https://stackoverflow.com/questions/34053043/the-condition-has-length-1-and-only-the-first-element-will-be-used-in-if-else) – jogo Jul 02 '18 at 07:43

1 Answers1

0

Assuming Data$City is a vector of city names, and also assuming that you want to check if at least one of those city names is present in a given list, you could:

  1. Store all valid city names into a character vector, namely validCities.
  2. Use the %in% operator between those two vectors in order to obtain a logical vector. This vector will be of the same length as the first one, and will say which ones of those cities are contained in the second vector.
  3. Use the sum function to verify if there is at least one positive, i.e., check if any of the cities contained in the first vector is present in the second vector.

Example below.

Data <- data.frame(City = c('Chennai', 'Delhi', 'Bhopal', 'Pune', 'Kolkata'));

validCities <- c('Mumbai', 'Delhi', 'Bengaluru');

if (sum(Data$City %in% validCities) > 0) {
    // Your code here.
}

Updated:

Now that you have provided your desired output, I can see that that's pretty easy. Don't stuck on loop-focused approaches, a data.frame can be easily selected and filtered by row, just provide a condition for those rows that you want to consider, and indicate what columns you want to retrieve or modify.

In this case, I'm selecting those rows which CITY is not one of the three provided, and I'm assigning a NA value to the CITY column:

data <- data.frame(CITY = c('Mumbai', 'Jaipur', 'Delhi', 'Chennai', 'Bengaluru'));

data[!(data$CITY %in% c('Mumbai', 'Delhi', 'Bengaluru')), 'CITY'] <- NA;

Output:

> data
       CITY
1    Mumbai
2      <NA>
3     Delhi
4      <NA>
5 Bengaluru

Also, you could simply remove the undesired rows, in which case the remaining rows would keep their original row name:

data <- data[data$CITY %in% c('Mumbai', 'Delhi', 'Bengaluru'), , drop = FALSE];

Output:

> data
       CITY
1    Mumbai
3     Delhi
5 Bengaluru
  • @KrishnAB That code makes little sense to me. The `while` loop condition is evaluating `Data$City` (I cannot see its declaration), but it is modifying `Data1$City` instead, so the `while` statement is going to iterate forever. Could you provide a functional explanation of what you want to do? – Indifferent Jul 02 '18 at 07:30
  • I have edited once more the entire thing @Indifferent .Please check – KrishnAB Jul 02 '18 at 07:43
  • @KrishnAB Your code has no sense at all. Have you tried it at least? You have a missing parenthesis in the second statement, and that `while` loop iterates forever (as I said). Again, what is the purpose of your code, in functional terms? What is the desired output? Whatever it is, chances are that you are tackling it wrong. – Indifferent Jul 02 '18 at 08:32
  • Kindly look into the desired output now and help with code for the desired output. @Indifferent – KrishnAB Jul 02 '18 at 09:18
  • @KrishnAB I've just updated my answer. Please, mark it as the correct one if it meets your needs. – Indifferent Jul 02 '18 at 10:13
  • What I want is along the sequence of nrow(DataO) i.e for loop(1 to 5), when Mumbai==Mumbai it should execute some statements(statements doesn't matter as along as I can't get the desired condition in while loop) and thereafter when Delhi==Delhi it should execute some statements and finally when Bengaluru==Bengaluru the execution should be done with(It should be end of for loop as well as while condition). The while loop isn't vectorized is pretty much evident. If you need those inner statements please let me know. Also please let me know why the while loop will keep on executing ? – KrishnAB Jul 02 '18 at 11:24