Roelalex1996
Roelalex1996

Reputation: 73

Why does R give a non-numeric argument error after summing values over different columns

I have the following table:

          FIPS             areaLite        demandPCRLITE         totalPCRLITE           domesticSW         industrialSW         irrigationSW
1         1001    1448089200.000000            0.0458999            0.0458999           0.00584974            0.0293009          5.67298e-05
2         1003    4280593864.000000            0.0596989            0.0596989            0.0071421            0.0415197           0.00017834
3         1005    2406889384.000000            0.0200443            0.0200443           0.00169389            0.0148862          8.55599e-05
4         1007    1584348144.000000            0.0274318            0.0274318           0.00246643            0.0179085          1.22481e-13
5         1009    1637652816.000000            0.0596849            0.0596849           0.00635245            0.0421242          1.82366e-05
6         1011    1600075856.000000            0.0114741            0.0114741           0.00131684           0.00701411          7.57648e-06
           livestockSW          domesticRGW        industrialRGW        irrigationRGW         livestockRGW         domesticNRGW
1          8.88859e-05          0.000956943           0.00600027          4.33649e-06          1.61736e-05          0.000624597
2           8.0345e-05           0.00134032           0.00713391          6.97545e-06          1.31466e-05          0.000333652
3          0.000111635          0.000253806           0.00200294          5.85125e-06          1.61015e-05          0.000113247
4          3.64766e-05          0.000648518           0.00438329          4.28058e-14          1.28159e-05          0.000236784
5          0.000327681           0.00135987           0.00848195          2.10108e-06          5.27083e-05          0.000136469
6          0.000119269          0.000416357           0.00211257          2.30538e-06          4.00352e-05          8.19511e-05
        industrialNRGW       irrigationNRGW        livestockNRGW
1           0.00299258          2.98772e-06          5.76049e-06
2           0.00192211          2.52641e-05          3.05178e-06
3          0.000858313          1.02581e-05          6.44633e-06
4           0.00173293                    0          6.10518e-06
5          0.000825706                    0          3.42567e-06
6          0.000350881          3.39371e-07          1.18479e-05

Now I want to add the values for example for SW. So I used the following code:

mutate(surfacewaterPCRLITE= domesticSW+industrialSW+irrigationSW+livestockSW)

I did this before with another table and it works perfectly fine. However, for some unknown reason I get this error:

Error: Problem with `mutate()` input `surfacewaterPCRLITE`.
x non-numeric argument to binary operator
ℹ Input `surfacewaterPCRLITE` is `domesticSW + industrialSW + irrigationSW + livestockSW`.

Why does it give this message? There are numbers in the columns right? Or is it literally trying to add domesticSW to industrialSW? How can I prevent this from happening that it starts at the second line?

dput(head(data):

structure(list(FIPS = c("        1001", "        1003", "        1005", 
"        1007", "        1009", "        1011"), areaLite = c("   1448089200.000000", 
"   4280593864.000000", "   2406889384.000000", "   1584348144.000000", 
"   1637652816.000000", "   1600075856.000000"), demandPCRLITE = c("           0.0458999", 
"           0.0596989", "           0.0200443", "           0.0274318", 
"           0.0596849", "           0.0114741"), totalPCRLITE = c("           0.0458999", 
"           0.0596989", "           0.0200443", "           0.0274318", 
"           0.0596849", "           0.0114741"), domesticSW = c("          0.00584974", 
"           0.0071421", "          0.00169389", "          0.00246643", 
"          0.00635245", "          0.00131684"), industrialSW = c("           0.0293009", 
"           0.0415197", "           0.0148862", "           0.0179085", 
"           0.0421242", "          0.00701411"), irrigationSW = c("         5.67298e-05", 
"          0.00017834", "         8.55599e-05", "         1.22481e-13", 
"         1.82366e-05", "         7.57648e-06"), livestockSW = c("         8.88859e-05", 
"          8.0345e-05", "         0.000111635", "         3.64766e-05", 
"         0.000327681", "         0.000119269"), domesticRGW = c("         0.000956943", 
"          0.00134032", "         0.000253806", "         0.000648518", 
"          0.00135987", "         0.000416357"), industrialRGW = c("          0.00600027", 
"          0.00713391", "          0.00200294", "          0.00438329", 
"          0.00848195", "          0.00211257"), irrigationRGW = c("         4.33649e-06", 
"         6.97545e-06", "         5.85125e-06", "         4.28058e-14", 
"         2.10108e-06", "         2.30538e-06"), livestockRGW = c("         1.61736e-05", 
"         1.31466e-05", "         1.61015e-05", "         1.28159e-05", 
"         5.27083e-05", "         4.00352e-05"), domesticNRGW = c("         0.000624597", 
"         0.000333652", "         0.000113247", "         0.000236784", 
"         0.000136469", "         8.19511e-05"), industrialNRGW = c("          0.00299258", 
"          0.00192211", "         0.000858313", "          0.00173293", 
"         0.000825706", "         0.000350881"), irrigationNRGW = c("         2.98772e-06", 
"         2.52641e-05", "         1.02581e-05", "                   0", 
"                   0", "         3.39371e-07"), livestockNRGW = c("         5.76049e-06", 
"         3.05178e-06", "         6.44633e-06", "         6.10518e-06", 
"         3.42567e-06", "         1.18479e-05")), row.names = c(NA, 
6L), class = "data.frame")

Upvotes: 0

Views: 73

Answers (3)

AnilGoyal
AnilGoyal

Reputation: 26238

As clear from dput your numeric values are actually stored as character and that's why error. First convert all numeric variables stored as characters to numeric. You may use mutate(across.. to convert all these at once. After this you may use rowwise and c_across which will be less verbose

df %>% rowwise() %>%
  mutate(surfacewaterPCRLITE = sum(as.numeric(c_across(ends_with('SW')))))

Upvotes: 0

Basti
Basti

Reputation: 1763

Your data are not numeric as checked with str(data), you should do :

data = data.frame(apply(data,2,function(x) as.numeric(x)))

data %>% mutate(surfacewaterPCRLITE= domesticSW+industrialSW+irrigationSW+livestockSW)

Upvotes: 1

Pedro Alencar
Pedro Alencar

Reputation: 1089

You should use the pipe command.

Try this:

df %>% mutate(surfacewaterPCRLITE= domesticSW+industrialSW+irrigationSW+livestockSW)

(where df is your dataframe)

cheers,

Upvotes: 1

Related Questions