首页 > 解决方案 > 为什么 R 在对不同列的值求和后会给出非数字参数错误

问题描述

我有下表:

          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

现在我想添加例如 SW 的值。所以我使用了以下代码:

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

我以前用另一张桌子做过这个,它工作得很好。但是,由于某些未知原因,我收到此错误:

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

为什么它会给出这个信息?列中有数字对吗?或者它是否真的试图将国内软件添加到工业软件中?我怎样才能防止这种情况从第二行开始发生?

输入(头(数据):

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")

标签: rdplyr

解决方案


您应该使用管道命令。

尝试这个:

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

df你的数据框在哪里)

干杯,


推荐阅读