rfor-loop

R: For loop, need to use i and i+1 in same line, giving error non-numeric argument to binary operator


I'm writing a script to split a csv file into a user defined number of files. I'm using For loops for the first time and ran into a problem I can't figure out how to solve. All but one line of the For loop works. When I replace that line with a temporary print command, the rest of the loop works fine.

When I try to run the script, I get the error code "non-numeric argument to binary operator". I know what's causing the error, but nothing I change in the For loop seems to fix the issue.

Here's an example of the For loop I wrote:

y <- as.numeric(readline("Enter number of files: "))
div <- nrow(df)/y
z <- c(0, seq(from = 1, to = 20, by = 2))
x <- z[y]
k <- 2
for(i in 1:x){
if(i %% 2 == 0){
assign(paste0("div", i), div * k)
k <- k + 1
} else {
assign(paste0("div", i), paste0("div", i - 1) + 1)
}}

It's supposed to be a simplified way of assigning the div values, instead of writing all div values for each y like so:

y = 4
div1 = div + 1
div2 = div * 2
div3 = div2 + 1
div4 = div * 3
div5 = div4 + 1

The div values (specific row numbers) will be used in another For loop (same error occurs) to split the original df into y number of chunks. (chunk2 = sample(df[div1:div2,]))

I know the error is coming from the line assign(paste0("div", i), paste0("div", i - 1) + 1) and is caused by the i - 1 and/or the + 1 at the end.

Things I have tried:

  1. incrementing i within the loop
i = i - 1
i_two <- paste0("div", i)
i = i + 1
assign(paste0("div", i), i_two + 1)
  1. asssigning a variable to i - 1
m = i - 1
assign(paste0("div", i), paste0("div", m) + 1)
  1. converting i between integer, numeric, and double in various combinations & orders
assign(paste0("div", i), paste0("div", as.integer(as.double(i) - 1)) + 1)
  1. combinations of converting i type and incrementing or assigning variable

I have read as many similar posts on this site as I could find (and that I could understand with my limited R knowledge & experience). I have noticed that a common cause for the error is that some defined variable is in the wrong type, when it should be numeric. I tried converting as many variables to numeric as I could, but that didn't seem to fix it either.

Nothing I've tried has worked, but I very well may have missed something. Any help is appreciated.

(Note: I'm trying to keep the script in base R & RTools. But, if necessary, I can use the tidyverse package. Also, I'm using my own environment in the script, so rest assured the variables aren't being saved to the global environment.)


Solution

  • Variable div changed to a list.

    y <- as.numeric(readline("Enter number of files: "))
    div <- list(list(),list())
    div[[1]] <- nrow(df)/y
    z <- c(0, seq(from = 1, to = 20, by = 2))
    x <- z[y]
    k <- 2
    for(i in 1:x){
    if(i %% 2 == 0){
    div[[2]][[i]] <- div[[1]] * k
    k <- k +1
    } else {
    div[[2]][[i]] <- div[[i-1]] + 1
    }}