title: How to overcome this error:Error in tapply(seq_len()
body: <p>I used the code below to get a linear regression prediction per row based on each species and it worked just fine: </p>
  
  <pre><code>R2 &lt;- do.call("rbind", as.list( by(iris, iris["Species"], transform, regress=predict(lm(Sepal.Length~Sepal.Width + Petal.Length + Petal.Width,data=iris),newdata=iris,type='response')))
  head(R2)
             Sepal.Length Sepal.Width Petal.Length Petal.Width   Species  regress
  setosa.1               5.1         3.5          1.4         0.2    setosa 5.021873
  setosa.2               4.9         3.0          1.4         0.2    setosa 4.676855
  setosa.3               4.7         3.2          1.3         0.2    setosa 4.745967
  setosa.4               4.6         3.1          1.5         0.2    setosa 4.814754
  setosa.5               5.0         3.6          1.4         0.2    setosa 5.090877
  setosa.6               5.4         3.9          1.7         0.4    setosa 5.404108
  </code></pre>
  
  <p>However, when I tried to implement it on another data set where I would like to have the prediction based on country name (I can't upload it) i got the following error:</p>
  
  <pre><code>Error in tapply(seq_len(156695L), list(Country = c(72L, 168L, 207L, 94L,  : 
    arguments must have same length.
  </code></pre>
  
  <p>How can I solve this issue? Is there a better code that will do the job?</p>
  
  <p>UPDATE:
  I added two data frames to better explain my need:
  I would like to have an <code>lm()</code> prediction per country as a new column for df1 based on the models per each country that should be based on data frame df.</p>
  
  <pre><code>    df &lt;- read.table(text = "target birds    wolfs     Country
                                  3        9         7 a
                                  3        8         4 b
                                  1        2         8 c
                                  1        2         3 a
                                  1        8         3 a
                                  6        1         2 a
                                  6        7         1 b
                                  6        1         5 c   ",header = TRUE)
  df1&lt;-read.table(text = "target birds    wolfs     Country
                           6        4         5 a
                           4        5         3 a
                           3        8         2 a
                           1        6         4 b
                           3        5         1 a
                           2        2         1 b
                           9        9         4 b
                           8        9         5 c
                           2        3         1 c",header = TRUE)
  </code></pre>
  
  <p>So the result should be as below where country "a" prediction should be based on the lm() model for that country ony and so on..( fake numbers for the last column - it's only for the data frame's structure)</p>
  
  <pre><code>df1&lt;-read.table(text = "target birds    wolfs     Country regress
                           6        4         5     a           5.2
                           4        5         3     a           5.4
                           3        8         2     a           4.8 
                           1        6         4     b           6.6 
                           3        5         1     a           5.0 
                           2        2         1     b           6.1
                           9        9         4     b           6.7
                           8        9         5     c           2.3
                           2        3         1     c           3.1 ",header = TRUE)
  </code></pre>