0

Create a new data frame that contains the results of the increase or decrease in the number of migrants for every province. - Hint: Subtract the 'To' column 'From' column and add a new column to reflect the change in a new data frame.

I would like to know in R, how to do the subtract function for a numerical column in a data frame with non-numerical values in a column.

When I use

diff(mig_rename,lag=1,difference=1)

I got the error as

Error in r[i1] - r[-length(r):-(length(r) - lag + 1L)] :
non-numeric argument to binary operator"

My data:

mig_rename <- read.table(text="
   From   To Migrants
1   PEI NFLD      255
2    NS NFLD     2380
3    NB NFLD     1140
4   QUE NFLD     2145
5   ONT NFLD     6295
6   MAN NFLD      215
7  SASK NFLD      185
8  ALTA NFLD      425
9    BC NFLD      425
10 NFLD  PEI      340
11   NS  PEI     1975
12   NB  PEI     1310
13  QUE  PEI      755
14  ONT  PEI     3060
15  MAN  PEI      400
16 SASK  PEI       95
17 ALTA  PEI      185
18   BC  PEI      330
19 NFLD   NS     3340
20  PEI   NS     2185
21   NB   NS     8310
22  QUE   NS     6090
23  ONT   NS    18805
24  MAN   NS     1825
25 SASK   NS      840
26 ALTA   NS     2000
27   BC   NS     2490
28 NFLD   NB     1740
29  PEI   NB     1335
30   NS   NB     7635
31  QUE   NB     9315
32  ONT   NB    12455
33  MAN   NB     1405
34 SASK   NB      480
35 ALTA   NB     1130
36   BC   NB     1195
37 NFLD  QUE     2235
38  PEI  QUE      635
39   NS  QUE     4350
40   NB  QUE     7905
41  ONT  QUE    48370
42  MAN  QUE     4630
43 SASK  QUE     1515
44 ALTA  QUE     3305
45   BC  QUE     4740
46 NFLD  ONT    17860
47  PEI  ONT     3570
48   NS  ONT    25730
49   NB  ONT    18550
50  QUE  ONT    99430
51  MAN  ONT    23785
52 SASK  ONT    11805
53 ALTA  ONT    17655
54   BC  ONT    21205
55 NFLD  MAN      680
56  PEI  MAN      265
57   NS  MAN     1655
58   NB  MAN     1355
59  QUE  MAN     4330
60  ONT  MAN    18245", header=TRUE)
user20650
  • 24,654
  • 5
  • 56
  • 91
Kalpana
  • 21
  • 1
  • 1
  • 1
    Welcome to Stack Overflow! Please take the [tour](https://stackoverflow.com/tour) and read through the [help center](http://stackoverflow.com/help), in particular [how to ask](https://stackoverflow.com/help/how-to-ask). Your best bet here is to do your research, search for related topics on SO, and give it a go. After doing more research and searching, post a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of your attempt and say specifically where you're stuck, which can help you get better answers. – help-info.de Jun 08 '19 at 14:37
  • See also [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – help-info.de Jun 08 '19 at 14:40
  • 2
    Mathematically; I don't think it makes a lot of sense to subtract a numeric number from a letter or word. I suspect your question is asking you to create a column of difference in the `Migrants` column by group – user20650 Jun 08 '19 at 14:47
  • https://stackoverflow.com/questions/14846547/calculate-difference-between-values-in-consecutive-rows-by-group might be useful – user20650 Jun 08 '19 at 14:49

0 Answers0