For loop not moving through iterations

Hello everyone,

Can someone help me with figuring out where i am making a mistake with my code. I want to filter data in one dataframe by matching value of its column with the value of another column one by one. And then calculate the proportion of the filtered data based on 1 and 0 in another column of the filtered data. But I am getting iterations for all obersations again and again and am getting only the same proportion everytime. My code is:

Thanks in advance!

its hard to start to reason about your code ...
i and j are looping variables, and you compare them directly inside a filter on a dataset ?
so you will either get all rows or no rows from your dataset based on whether i is smalled than j or not.

probably not what you want ?

I'd approach that differently. Here's an example:

library(tidyverse)

df1 <- tibble(
  group = c("A", "B", "C"),
  value = c(1, 2, 3)
)

df2 <- tibble(
  group = c("D", "C", "B"),
  value = c(4, 3, 2)
  )

inner_join(df1, df2, by = "group") |> 
  mutate(prop = value.x / value.y) |>
  select(group, prop)

which gives:


# A tibble: 2 × 2
  group  prop
  <chr> <dbl>
1 B         1
2 C         1

In order for your code to be reproducible, you would have to supply all the values necessary to run your code and reproduce the issue. Namely, the lengths of u2 and u, and the contents of df, which you can share by running dput(df), and the posting the output here, between a pair of triple backticks, like this:

```
paste output
```

here's the df:

subject.id u group
1 0.026 0
2 0.125 0
3 0.081 0
4 0.000 0
5 0.044 0
6 0.006 0
7 0.011 1
8 0.000 0
9 0.000 0
10 0.045 0
11 0.003 0
12 0.000 0
13 0.053 1
14 0.030 0
15 0.022 1
16 0.067 0
17 0.003 1
18 0.021 0
19 0.004 0
20 0.013 0
21 0.002 0
22 0.012 0
23 0.014 0
24 0.017 1
25 0.006 0
26 0.030 0
27 0.000 0
28 0.250 0
29 0.000 0
30 0.005 1

and u2:

u2
0.0000
0.0006
0.0012
0.0013
0.0013
0.0015
0.0021
0.0022
0.0028
0.0028
0.0029
0.0032
0.0034
0.0038
0.0038
0.0046
0.0046
0.0047
0.0048
0.0050
0.0050
0.0051
0.0054
0.0056
0.0113
0.0115
0.0120
0.0270
0.0282
0.0283
0.0294
0.0488
0.0526
0.0550
0.0667
0.0675
0.0714
0.2500
0.2727

I want to compare u2 with u (in df). Take u2 one by one and filter data in df with u>=u2 to compute proportions based on binary response in the group column of df.
Any help will be appreciated! Thanks!

I suspect line 841 should actually be

df_net <- filter(df, j >= u2[i])

But it will be difficult to help properly until you follow the instructions from @dromano correctly:

  1. Run dput(df) and paste the output between a pair of triple backticks.
  2. Run dput(u2) and paste the output between a pair of triple backticks.
  3. Copy and paste the section of code you photographed into the post between a pair of triple backticks. A photograph of the code is not appropriate.
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.