简体   繁体   中英

R for loop returns NULL instead of expected values

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM