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 ?