1

I'm just genuinely confused here. So here is some code that works fine:

 insertion3=county$rpc85_weighted.mean[1]-county$rpc85_weighted.mean
  county$changed<-insertion3

I get the column with the values I want. But for some reason, when I make it into a function like this:

differences=function(county) {
  insertion3=county$rpc85_weighted.mean[1]-county$rpc85_weighted.mean
  county$changed<-insertion3
  return(county)
}

It stops at the <-insertion3 line and gives me the error that

Error in `$<-.data.frame`(`*tmp*`, changed, value = integer(0)) : 
  replacement has 0 rows, data has 94 

What's so different about it being a function

Phil
  • 7,287
  • 3
  • 36
  • 66
Nadka
  • 11
  • 3
  • insertion3 is a number or a vector? If yes, in this assigment, county$changed<-insertion3, LHS is a vector, RHS is a number. May be you can replace insertion3 by rep(insertion3 , dim(county)[1]) – TrungDung Nov 27 '20 at 13:06
  • 1
    Please post a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – eastclintw00d Nov 27 '20 at 14:09

0 Answers0