Why does my 'for' loop break due to a boolean set-membership check, and how do I fix it?

Nel

I am recoding data in R and want a basic for-loop with the following properties:

  • Assess every value in a vector.

  • If the value is not a member of a fixed set, then recode the value to 0.

For the following example code, that would mean that all 50 values in the array 'data' are assessed and will be recoded to 0, 1, 3, or 5. However, the loop stops early and only the first few numbers are recoded.

set = c(1, 3, 5)

data<-round(runif(n=50, min=1, max=8), 0)
data #initial data set

for (x in data) {
  if (!(data[x] %in% set)) {
    data[x]<- 0
  }
}

data #recoded data

My troubleshooting: I tried to insert an else..next statement to keep the loop going.

TarJae

The correct for loop is:

Use the indices of data. In your actual for loop you are using the values:

for (i in seq_along(data)) {
  if (!(data[i] %in% set)) {
    data[i] <- 0
  }
}

#data
#recoded data
 [1] 1 0 0 0 3 0 0 0 0 5 3 0 3 3 0 0 0 1 0 0 5 0 0 0 0 0 5 0 0 0
[31] 0 0 5 0 3 1 0 5 0 1 0 1 3 0 0 0 5 0 3 0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why doesn't my for loop actually loop? I do not have a break function, and expected it to loop but it does not

Why is my while going in a infinity loop? How do I fix it?

If my OEMCP is set to 437 then why is my cmd.exe default codepage 932? and how do I fix this?

Why does uint break my for loop?

How does suspend fix my touchpad and break my wifi, and how can I take control over this?

Why does mongodb change my date formats to long number and how do I fix it?

Why does Bootstrap destroys my normal css layout and how do I fix it?

Why does my tool output overwrite itself and how do I fix it?

Why does decreasing the size of the renderer offset my click events - How do I fix this

Why does my DataGridView resist displaying the bottom data and how do I fix it?

Why does my boolean for my loop keep being set back to True?

Why does my If/Else loop break out of my For loop?

Why does my variable set in a do loop disappear? (unix shell)

How do I fix my iframe image that caused my website format to break?

How do I fix my For loop and Random problem in Android?

How do I fix my random number while loop?

How do I fix an off by one error for my for loop?

The box of GUILayout.TextField disappears when I try to center my text, why does it do that and how do I fix it?

Why does this @JsonIgnore fix my infinite loop?

Why does my Makefile not compile and how can I fix it?

Why is my actionbar not showing and how do I fix it?

Why are months in my date slicer repeating? How do I fix it?

How do I fix my Javascript function that does not work?

Google Accounts has listed my device twice due to switching to Ubuntu. How do I fix it?

How do I fix fix error in plotting due to indexing?

If my Pharo image goes into an infinite loop, how do I break out of the loop?

Why does Bazel under-link and how do I fix it?

Why does Vim delay on this remapped key? And how do I fix it?

How do I turn sets into indicators of set membership?