as()
R/SoilProfileCollection-coercion.R
coercion-methods.Rd
SoilProfileCollections can be coerced to other R object types using as(spc, 'type')
.
Possible endpoints include: list
, data.frame
, SpatialPointsDataFrame
and SpatialPoints
.
# S4 method for class 'SoilProfileCollection'
as.data.frame(x)
list
data.frame
tbl_df
data.table
SpatialPointsDataFrame
sf
SpatialPoints
# load example data stored as SoilProfileCollection
data(sp5)
# sp5
str(sp5)
#> Formal class 'SoilProfileCollection' [package "aqp"] with 8 slots
#> ..@ idcol : chr "soil"
#> ..@ hzidcol : chr "hzID"
#> ..@ depthcols : chr [1:2] "top" "bottom"
#> ..@ metadata :List of 8
#> .. ..$ aqp_df_class : chr "data.frame"
#> .. ..$ aqp_group_by : chr ""
#> .. ..$ aqp_hzdesgn : chr "name"
#> .. ..$ aqp_hztexcl : chr ""
#> .. ..$ depth_units : chr "cm"
#> .. ..$ stringsAsFactors: logi FALSE
#> .. ..$ citation : chr "F. Carre, M.C. Girard. 2002. Quantitative mapping of soil types based on regression kriging of taxonomic distan"| __truncated__
#> .. ..$ dateAdded : Date[1:1], format: "2024-04-01"
#> ..@ horizons :'data.frame': 1539 obs. of 19 variables:
#> .. ..$ soil : chr [1:1539] "soil1" "soil1" "soil1" "soil1" ...
#> .. ..$ sand : num [1:1539] 32.3 29 34.9 38.2 32.2 25.2 24.4 24 21.2 18.4 ...
#> .. ..$ silt : num [1:1539] 10.9 11.2 11.6 10.9 10.8 ...
#> .. ..$ clay : num [1:1539] 52.8 58.2 51.9 49.7 55.6 ...
#> .. ..$ R25 : num [1:1539] 0.41 0.31 0.31 0.31 0.55 0.43 0.44 0.34 0.35 0.35 ...
#> .. ..$ G25 : num [1:1539] 0.38 0.28 0.28 0.28 0.46 0.37 0.37 0.28 0.27 0.27 ...
#> .. ..$ B25 : num [1:1539] 0.34 0.25 0.25 0.25 0.36 0.3 0.31 0.23 0.19 0.19 ...
#> .. ..$ pH : num [1:1539] 6.85 8.08 9.2 9.29 9.24 8.26 8.95 9.34 9.24 9.13 ...
#> .. ..$ EC : num [1:1539] 24 10 23.9 41.6 73.6 14.5 16.1 26.1 43.8 61.5 ...
#> .. ..$ CaCO3 : num [1:1539] 0.05 0.05 0.3 0.4 0.7 0.1 0.5 1 1.05 1.1 ...
#> .. ..$ C : num [1:1539] 2.29 0.9 0.74 0.35 0.25 1.06 0.66 0.58 0.47 0.36 ...
#> .. ..$ Ca : num [1:1539] 183 234 231 168 203 ...
#> .. ..$ Mg : num [1:1539] 146 172 166 152 167 ...
#> .. ..$ Na : num [1:1539] 19 34.1 70.2 81.7 100 ...
#> .. ..$ top : int [1:1539] 0 8 25 55 100 0 10 25 40 70 ...
#> .. ..$ bottom : int [1:1539] 8 25 55 100 267 10 25 40 70 100 ...
#> .. ..$ hzID : chr [1:1539] "1" "2" "3" "4" ...
#> .. ..$ soil_color: chr [1:1539] "#696157" "#4F4740" "#4F4740" "#4F4740" ...
#> .. ..$ name : chr [1:1539] "H1" "H2" "H3" "H4" ...
#> ..@ site :'data.frame': 296 obs. of 1 variable:
#> .. ..$ soil: chr [1:296] "soil1" "soil10" "soil100" "soil101" ...
#> ..@ diagnostic :'data.frame': 0 obs. of 0 variables
#> ..@ restrictions:'data.frame': 0 obs. of 0 variables
# list output
str(as(sp5, 'list'))
#> List of 8
#> $ idcol : chr "soil"
#> $ hzidcol : chr "hzID"
#> $ depthcols : chr [1:2] "top" "bottom"
#> $ metadata :List of 8
#> ..$ aqp_df_class : chr "data.frame"
#> ..$ aqp_group_by : chr ""
#> ..$ aqp_hzdesgn : chr "name"
#> ..$ aqp_hztexcl : chr ""
#> ..$ depth_units : chr "cm"
#> ..$ stringsAsFactors: logi FALSE
#> ..$ citation : chr "F. Carre, M.C. Girard. 2002. Quantitative mapping of soil types based on regression kriging of taxonomic distan"| __truncated__
#> ..$ dateAdded : Date[1:1], format: "2024-04-01"
#> $ horizons :'data.frame': 1539 obs. of 19 variables:
#> ..$ soil : chr [1:1539] "soil1" "soil1" "soil1" "soil1" ...
#> ..$ sand : num [1:1539] 32.3 29 34.9 38.2 32.2 25.2 24.4 24 21.2 18.4 ...
#> ..$ silt : num [1:1539] 10.9 11.2 11.6 10.9 10.8 ...
#> ..$ clay : num [1:1539] 52.8 58.2 51.9 49.7 55.6 ...
#> ..$ R25 : num [1:1539] 0.41 0.31 0.31 0.31 0.55 0.43 0.44 0.34 0.35 0.35 ...
#> ..$ G25 : num [1:1539] 0.38 0.28 0.28 0.28 0.46 0.37 0.37 0.28 0.27 0.27 ...
#> ..$ B25 : num [1:1539] 0.34 0.25 0.25 0.25 0.36 0.3 0.31 0.23 0.19 0.19 ...
#> ..$ pH : num [1:1539] 6.85 8.08 9.2 9.29 9.24 8.26 8.95 9.34 9.24 9.13 ...
#> ..$ EC : num [1:1539] 24 10 23.9 41.6 73.6 14.5 16.1 26.1 43.8 61.5 ...
#> ..$ CaCO3 : num [1:1539] 0.05 0.05 0.3 0.4 0.7 0.1 0.5 1 1.05 1.1 ...
#> ..$ C : num [1:1539] 2.29 0.9 0.74 0.35 0.25 1.06 0.66 0.58 0.47 0.36 ...
#> ..$ Ca : num [1:1539] 183 234 231 168 203 ...
#> ..$ Mg : num [1:1539] 146 172 166 152 167 ...
#> ..$ Na : num [1:1539] 19 34.1 70.2 81.7 100 ...
#> ..$ top : int [1:1539] 0 8 25 55 100 0 10 25 40 70 ...
#> ..$ bottom : int [1:1539] 8 25 55 100 267 10 25 40 70 100 ...
#> ..$ hzID : chr [1:1539] "1" "2" "3" "4" ...
#> ..$ soil_color: chr [1:1539] "#696157" "#4F4740" "#4F4740" "#4F4740" ...
#> ..$ name : chr [1:1539] "H1" "H2" "H3" "H4" ...
#> $ site :'data.frame': 296 obs. of 1 variable:
#> ..$ soil: chr [1:296] "soil1" "soil10" "soil100" "soil101" ...
#> $ diagnostic :'data.frame': 0 obs. of 0 variables
#> $ restrictions:'data.frame': 0 obs. of 0 variables
# data.frame output
str(as(sp5, 'data.frame'))
#> 'data.frame': 1539 obs. of 19 variables:
#> $ soil : chr "soil1" "soil1" "soil1" "soil1" ...
#> $ sand : num 32.3 29 34.9 38.2 32.2 25.2 24.4 24 21.2 18.4 ...
#> $ silt : num 10.9 11.2 11.6 10.9 10.8 ...
#> $ clay : num 52.8 58.2 51.9 49.7 55.6 ...
#> $ R25 : num 0.41 0.31 0.31 0.31 0.55 0.43 0.44 0.34 0.35 0.35 ...
#> $ G25 : num 0.38 0.28 0.28 0.28 0.46 0.37 0.37 0.28 0.27 0.27 ...
#> $ B25 : num 0.34 0.25 0.25 0.25 0.36 0.3 0.31 0.23 0.19 0.19 ...
#> $ pH : num 6.85 8.08 9.2 9.29 9.24 8.26 8.95 9.34 9.24 9.13 ...
#> $ EC : num 24 10 23.9 41.6 73.6 14.5 16.1 26.1 43.8 61.5 ...
#> $ CaCO3 : num 0.05 0.05 0.3 0.4 0.7 0.1 0.5 1 1.05 1.1 ...
#> $ C : num 2.29 0.9 0.74 0.35 0.25 1.06 0.66 0.58 0.47 0.36 ...
#> $ Ca : num 183 234 231 168 203 ...
#> $ Mg : num 146 172 166 152 167 ...
#> $ Na : num 19 34.1 70.2 81.7 100 ...
#> $ top : int 0 8 25 55 100 0 10 25 40 70 ...
#> $ bottom : int 8 25 55 100 267 10 25 40 70 100 ...
#> $ hzID : chr "1" "2" "3" "4" ...
#> $ soil_color: chr "#696157" "#4F4740" "#4F4740" "#4F4740" ...
#> $ name : chr "H1" "H2" "H3" "H4" ...
# Spatial Objects
# make some random coordinate data for each profile
sp5$x <- sp5$y <- rnorm(length(sp5))
initSpatial(sp5, crs = "OGC:CRS84") <- ~ x + y
if (requireNamespace("sf")) {
# sf output
str(as(sp5, 'sf'))
# SpatialPointsDataFrame output
str(as(sp5, 'SpatialPointsDataFrame'))
# SpatialPoints output
str(as(sp5, 'SpatialPoints'))
}
#> Loading required namespace: sf
#> only site data are extracted
#> Classes ‘sf’ and 'data.frame': 296 obs. of 4 variables:
#> $ soil : chr "soil1" "soil10" "soil100" "soil101" ...
#> $ y : num 0.524 1.309 0.795 -0.597 -0.779 ...
#> $ x : num 0.524 1.309 0.795 -0.597 -0.779 ...
#> $ geometry:sfc_POINT of length 296; first list element: 'XY' num 0.524 0.524
#> - attr(*, "sf_column")= chr "geometry"
#> - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA
#> ..- attr(*, "names")= chr [1:3] "soil" "y" "x"
#> only site data are extracted
#> Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
#> ..@ data :'data.frame': 296 obs. of 3 variables:
#> .. ..$ soil: chr [1:296] "soil1" "soil10" "soil100" "soil101" ...
#> .. ..$ y : num [1:296] 0.524 1.309 0.795 -0.597 -0.779 ...
#> .. ..$ x : num [1:296] 0.524 1.309 0.795 -0.597 -0.779 ...
#> ..@ coords.nrs : num(0)
#> ..@ coords : num [1:296, 1:2] 0.524 1.309 0.795 -0.597 -0.779 ...
#> .. ..- attr(*, "dimnames")=List of 2
#> .. .. ..$ : NULL
#> .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
#> ..@ bbox : num [1:2, 1:2] -2.58 -2.58 3.18 3.18
#> .. ..- attr(*, "dimnames")=List of 2
#> .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
#> .. .. ..$ : chr [1:2] "min" "max"
#> ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
#> .. .. ..@ projargs: chr "+proj=longlat +datum=WGS84 +no_defs"
#> .. .. ..$ comment: chr "GEOGCRS[\"WGS 84\",\n DATUM[\"World Geodetic System 1984\",\n ELLIPSOID[\"WGS 84\",6378137,298.257223"| __truncated__
#> only site data are extracted
#> Formal class 'SpatialPoints' [package "sp"] with 3 slots
#> ..@ coords : num [1:296, 1:2] 0.524 1.309 0.795 -0.597 -0.779 ...
#> .. ..- attr(*, "dimnames")=List of 2
#> .. .. ..$ : NULL
#> .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
#> ..@ bbox : num [1:2, 1:2] -2.58 -2.58 3.18 3.18
#> .. ..- attr(*, "dimnames")=List of 2
#> .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
#> .. .. ..$ : chr [1:2] "min" "max"
#> ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
#> .. .. ..@ projargs: chr "+proj=longlat +datum=WGS84 +no_defs"
#> .. .. ..$ comment: chr "GEOGCRS[\"WGS 84\",\n DATUM[\"World Geodetic System 1984\",\n ELLIPSOID[\"WGS 84\",6378137,298.257223"| __truncated__