0
i<-c(1:44)
diff_arbeitnehmer <- for(x in i){if(x == 44) {diff_arbeitnehmer[x] <- 0} else{diff_arbeitnehmer[x] <- 100/erwerbstaetige[x,2]*erwerbstaetige[x,4]-100/erwerbstaetige[x+1,2]*erwerbstaetige[x+1,4]}}

My data frame has 44 entriess

I am using R script could someone tell me what could be the reason?

I am lost with this

Rapiz
  • 161
  • 2
  • 2
  • 9
  • The `for` statement only ever returns `NULL` (as documented by the `?Control` help page). You never assign the result of `for` to anything. There are likely better ways to do this. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jan 15 '20 at 19:29
  • but I am literally telling R to assign in every step – Rapiz Jan 15 '20 at 19:32
  • But when the loop is over, you are assigning the result of the `for` (which is `NULL`) back to the object: `diff_arbeitnehmer <- for(...) {...}`. It doesn't matter what you do in the loop. After you run a command like that `diff_arbeitnehmer` is always going to be `NULL`. – MrFlick Jan 15 '20 at 19:34
  • so what commands would be better for such a simple problem? – Rapiz Jan 15 '20 at 19:34
  • Well, like I mentioned, it's much easier to help with a reproducible example with sample input and desired output so we can see what the real problem is rather than just your attempts to solve it. You can at least start by not assigning the results of the `for` loop to anything and initializing your result vector before the loop. But it's pretty rare to need an explicit `for` loop in R. – MrFlick Jan 15 '20 at 19:46

1 Answers1

0

I can't run your code because I don't have your data frame, but maybe the reason is because you are trying to assing a for loop into the variable diff_arbeitnehmer. I did this change and hope that know it works:

i<-c(1:44)
diff_arbeitnehmer <- c()
for(x in i){
  if(x == 44){
    diff_arbeitnehmer[x] <- 0
  } else{
      diff_arbeitnehmer[x] <- 100/erwerbstaetige[x,2]*erwerbstaetige[x,4]-100/erwerbstaetige[x+1,2]*erwerbstaetige[x+1,4]
  }
}

An advice is to take a look if the assignment in the last condition is right, maybe you need to put some parenthesis.

Filipe Lauar
  • 434
  • 3
  • 8
  • aaaaaaaaah damn hahahhah yeah I've literally assigned the for loop to that geez, thanks! – Rapiz Jan 15 '20 at 19:39