I'm trying to calculate the row sum for four columns in a dataframe. The dataframe was imported from an ESRI ArcGIS server and when I look at the structure of the columns I want to sum they all are defined as numeric but I still get the error 'x' must be numeric.
library(sf)
library(dplyr)
STTH <- st_read("https://services.arcgis.com/xxxx/ArcGIS/rest/services/service_xxxx/FeatureServer/")
STTH$projectsum <- rowSums(STTH [,19:22], na.rm = TRUE)
My dataframe looks like this for the columns 19:22
base_ST_funding | base_stlandowner_funding | base__section6 | base_partners |
---|---|---|---|
1200 | NA | 2100 | 800 |
1200 | 200 | 3200 | NA |
1200 | NA | NA | 210 |
1200 | 350 | NA | NA |
And when I call str(STTH) I get this for columns 19:22
$ base_ST_funding : int 2950 NA NA NA 4905 NA 100 NA NA NA ...
$ base_stlandowner_funding : int 3300 NA NA NA 20981 NA NA NA NA NA ...
$ base_section6 : int NA NA NA NA 40364 NA NA NA NA NA ...
$ base_partners : int NA NA NA NA 25000 NA NA NA NA NA ...
CodePudding user response:
It is because the object is a sf
as well as data.frame
. When we select the columns, the geometry columns are automatically selected. We could convert to data.frame
, select the columns and get the rowSums
STTH$projectsum <- rowSums(as.data.frame(STTH)[, 19:22], na.rm = TRUE)
-reproducible example
> library(sf)
> nc <- st_read(system.file("shape/nc.shp", package="sf"))
> str(nc)
Classes ‘sf’ and 'data.frame': 100 obs. of 15 variables:
$ AREA : num 0.114 0.061 0.143 0.07 0.153 0.097 0.062 0.091 0.118 0.124 ...
$ PERIMETER: num 1.44 1.23 1.63 2.97 2.21 ...
$ CNTY_ : num 1825 1827 1828 1831 1832 ...
$ CNTY_ID : num 1825 1827 1828 1831 1832 ...
$ NAME : chr "Ashe" "Alleghany" "Surry" "Currituck" ...
$ FIPS : chr "37009" "37005" "37171" "37053" ...
$ FIPSNO : num 37009 37005 37171 37053 37131 ...
$ CRESS_ID : int 5 3 86 27 66 46 15 37 93 85 ...
$ BIR74 : num 1091 487 3188 508 1421 ...
$ SID74 : num 1 0 5 1 9 7 0 0 4 1 ...
$ NWBIR74 : num 10 10 208 123 1066 ...
$ BIR79 : num 1364 542 3616 830 1606 ...
$ SID79 : num 0 3 6 2 3 5 2 2 2 5 ...
$ NWBIR79 : num 19 12 260 145 1197 ...
$ geometry :sfc_MULTIPOLYGON of length 100; first list element: List of 1
..$ :List of 1
.. ..$ : num [1:27, 1:2] -81.5 -81.5 -81.6 -81.6 -81.7 ...
..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
- attr(*, "sf_column")= chr "geometry"
- attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
..- attr(*, "names")= chr [1:14] "AREA" "PERIMETER" "CNTY_" "CNTY_ID" ...
> nc[, 1:4]
Simple feature collection with 100 features and 4 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
Geodetic CRS: NAD27
First 10 features:
AREA PERIMETER CNTY_ CNTY_ID geometry
1 0.114 1.442 1825 1825 MULTIPOLYGON (((-81.47276 3...
2 0.061 1.231 1827 1827 MULTIPOLYGON (((-81.23989 3...
3 0.143 1.630 1828 1828 MULTIPOLYGON (((-80.45634 3...
4 0.070 2.968 1831 1831 MULTIPOLYGON (((-76.00897 3...
5 0.153 2.206 1832 1832 MULTIPOLYGON (((-77.21767 3...
6 0.097 1.670 1833 1833 MULTIPOLYGON (((-76.74506 3...
7 0.062 1.547 1834 1834 MULTIPOLYGON (((-76.00897 3...
8 0.091 1.284 1835 1835 MULTIPOLYGON (((-76.56251 3...
9 0.118 1.421 1836 1836 MULTIPOLYGON (((-78.30876 3...
10 0.124 1.428 1837 1837 MULTIPOLYGON (((-80.02567 3...
> rowSums(nc[, 1:4], na.rm = TRUE)
Error in rowSums(nc[, 1:4], na.rm = TRUE) : 'x' must be numeric
> nc$newCol <- rowSums(as.data.frame(nc)[, 1:4], na.rm = TRUE)
> nc
Simple feature collection with 100 features and 15 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
Geodetic CRS: NAD27
First 10 features:
AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79
1 0.114 1.442 1825 1825 Ashe 37009 37009 5 1091 1 10 1364 0 19
2 0.061 1.231 1827 1827 Alleghany 37005 37005 3 487 0 10 542 3 12
3 0.143 1.630 1828 1828 Surry 37171 37171 86 3188 5 208 3616 6 260
4 0.070 2.968 1831 1831 Currituck 37053 37053 27 508 1 123 830 2 145
5 0.153 2.206 1832 1832 Northampton 37131 37131 66 1421 9 1066 1606 3 1197
6 0.097 1.670 1833 1833 Hertford 37091 37091 46 1452 7 954 1838 5 1237
7 0.062 1.547 1834 1834 Camden 37029 37029 15 286 0 115 350 2 139
8 0.091 1.284 1835 1835 Gates 37073 37073 37 420 0 254 594 2 371
9 0.118 1.421 1836 1836 Warren 37185 37185 93 968 4 748 1190 2 844
10 0.124 1.428 1837 1837 Stokes 37169 37169 85 1612 1 160 2038 5 176
geometry newCol
1 MULTIPOLYGON (((-81.47276 3... 3651.556
2 MULTIPOLYGON (((-81.23989 3... 3655.292
3 MULTIPOLYGON (((-80.45634 3... 3657.773
4 MULTIPOLYGON (((-76.00897 3... 3665.038
5 MULTIPOLYGON (((-77.21767 3... 3666.359
6 MULTIPOLYGON (((-76.74506 3... 3667.767
7 MULTIPOLYGON (((-76.00897 3... 3669.609
8 MULTIPOLYGON (((-76.56251 3... 3671.375
9 MULTIPOLYGON (((-78.30876 3... 3673.539
10 MULTIPOLYGON (((-80.02567 3... 3675.552