I have a stake in raster data of Landsat and I want to extract the values of them by employing SpatialPointData in R and then plot the extracted values with the associated variables in SpatialPointData and finally, I want to exported the extracted data along with the variable in the attribute of the spatial point object. I have used extract function to do so but the problem is that after extracting I got several errors every time and sometimes it works but it gives me only a data frame as a matrix which I cant match them with observation point.
My scripts
#raster
lsat <- stack(b1,b2,b3,b4,b5,b6_1,b6_2,b7)
#SpatialPoint
soil_sp=SpatialPoints(cbind(soil.clean2$x,soil.clean2$y))
Extrcat the value from stack layer
soil_sp$ref<- extract(lsat2, soil_sp)
plot the extrced value and observed value in point data for each band
plot( soil_sp$ref ~., data=soil_sp)
Finally, I want to export the extracted value along with variable in point data in a single data frame or SpatialPointsDataFrame.
The solution was to use SpatialPointDataFrame to extract the value of stack raster and then write the result into a CSV file or whatever you would like.
implementation:
Crerate SpatialPointDataFram out of SpatialPointData
soil_spdf=SpatialPointsDataFrame(coords = soil_sp,data = soil.clean2,
proj4string = soil_crs)
Extrcat the values.
soil_spdf$ref <- extract(lsat2 , soil_spdf)
write the data in CSV file in the desired directory on your PC.
write.csv(x =soil_spdf,file ="C:/lsat2.csv")
Related
I'm trying to extract the data matrix computed by Signac r package when running RegionMatrix(). I know I can plot the data with RegionHeatmap(), but I want to plot the data with another package.
So, if I created a region matrix like this:
my_object <- RegionMatrix(my_object
, key = "my_region_matrix"
, regions = StringToGRanges(top_cluster_genes$gene)
, upstream = 2500, downstream = 2500)
How do I go about extracting the data stored within the object under the key "my_region_matrix"? I know it's a pretty basic question, but I've been browsing the returned object and can't find any assay/matrix with that key name. (I'm putting this under Seurat because apparently there is no tag for Signac and the object is a Seurat object anyway)
I'm trying to use DESeq2's PCAPlot function in a meta-analysis of data.
Most of the files I have received are raw counts pre-normalization. I'm then running DESeq2 to normalize them, then running PCAPlot.
One of the files I received does not have raw counts or even the FASTQ files, just the data that has already been normalized by DESeq2.
How could I go about importing this data (non-integers) as a DESeqDataSet object after it has already been normalized?
Consensus in vignettes and other comments seems to be that objects can only be constructed from matrices of integers.
I was mostly concerned with getting the format the same between plots. Ultimately, I just used a workaround to get the plots looking the same via ggfortify.
If anyone is curious, I just ended up doing this. Note, the "names" file is just organized like the meta file for colData for building a DESeq object from DESeqDataSetFrom Matrix, but I changed the name of the design column from "conditions" to "group" so it would match the output of PCAplot. Should look identical.
library(ggfortify)
data<-read.csv('COUNTS.csv',sep = ",", header = TRUE, row.names = 1)
names<-read.csv("NAMES.csv")
PCA<-prcomp(t(data))
autoplot(PCA, data = names, colour = "group", size=3)
I am trying to get the wind exposure for infrastructures. I have a dataset with their latitude and longitude.
The NetCDF file gives daily near surface wind speed data projections for the year 2058. It can be downloaded with the following URL: http://esg-dn2.nsc.liu.se/thredds/fileServer/esg_dataroot1/cmip6data/CMIP6/ScenarioMIP/EC-Earth-Consortium/EC-Earth3/ssp585/r1i1p1f1/day/sfcWind/gr/v20200310/sfcWind_day_EC-Earth3_ssp585_r1i1p1f1_gr_20580101-20581231.nc
I have tried the following loop to get the average wind speed for each location (their closest grid point):
sfcWind_filepath<-paste0("sfcWind_day_EC-Earth3_ssp585_r1i1p1f1_gr_20580101-20581231.nc")
sfcWind_output<-nc_open(sfcWind_filepath)
lon<-ncvar_get(sfcWind_output,varid = "lon")
lat<-ncvar_get(sfcWind_output,varid = "lat")
sfcWind_time<-nc.get.time.series(sfcWind_output,v = "sfcWind",time.dim.name = "time")
sfcWind<-ncvar_get(sfcWind_output, "sfcWind")
for(i in 1:nrow(Infrast))
{sfcWind<-rep(i,nrow(Infrast))
x<-Infrast[i,4]
y<-Infrast[i,3]
Infrast[i,12]<-mean(sfcWind[which.min(abs(lon - (x))),
which.min(abs(lat - (y))),
c(which(format(sfcWind_time, "%Y-%m-%d") == "2058-01-01"):which(format(sfcWind_time, "%Y-%m-%d") == "2058-12-31"))])
}
Where Infrast is my dataset of infrastructures, their latitude is in column 3 and longitude in column 4, an I want the output to be saved at the 12th column of my dataset.
I get the following error:
Error in sfcWind[which.min(abs(lon - (x))), which.min(abs(lat - (y))), :
incorrect number of dimensions
I used this code before to get the average of projected temperatures and it worked just fine. The NetCDF file had the same dimensions than this one (lat, lon, time). This is why I don't understand the error here.
I am quite new to R and I just started to work with NetCDF files, any help or suggestion would be appreciated.
I'm also relative new to R too, but one possible way is to use the cmsaf package. Here, you can use the selpoint or selpoint.multi function to extract the time series of a variable at a specific location or for multiple locations. All you would need is the list of lat/lon coordinates for your desired locations. It will then make a new netcdf or csv file for the output. Then you could calculate the average from the extracted point data. There probably is a better and more efficient way but hopefully that might help.
NB I'm unable to test this, because no reproducible example was provided.
Nonetheless, this should work.
First open the file as a raster brick
library(raster)
sfcWind_output <- brick(sfcWind_filepath, varname="sfcWind")
Now you can extract values using coordinates like this
extract(sfcWind_output, cbind(lon,lat))
I have created a "ppp" objects in R spatstat package and inserted them into a hyper frame I would like to run test later but how do I save them such that I can access them from my computer every time I need
2. I have imported a text comma-delimited (txt), land cover file and converted them into "ppp" and "im" objects as required in the spatstat package. after conversion, I created a data frame from these two files
library(maptools)
library(spatstat)
point.pattern <- read.table("occ.txt", header=TRUE)
shape <- readshapespatial("polygon")
use shape file as a window by using the as.own function
ow <- as.owin(shape)
convert the txt file into a ppp object.
pattern.ppp <- as.ppp(point.pattern, ow, fatal=TR)
imported the raster file and convert into im object
raster <- raster("land cover.tif")
raster.im <- as.im(raster)
create a hyper frame hyper
frame <- hyperframe(X=pattern.ppp, Y=list(raster.im)
write and save the hyper frame data now
I completely do not know the function to use in order to save my hyper frame data such that I can access them any time from my computer minus rewriting the codes every time
You can save any R object using saveRDS():
saveRDS(frame, "your_filename.rds")
Later you can read it back in with readRDS():
frame <- readRDS("your_filename.rds")
Using leaflet, I'm trying to plot some lines and set their color based on a 'speed' variable. My data start at an encoded polyline level (i.e. a series of lat/long points, encoded as an alphanumeric string) with a single speed value for each EPL.
I'm able to decode the polylines to get lat/long series of (thanks to Max, here) and I'm able to create segments from those series of points and format them as a SpatialLines object (thanks to Kyle Walker, here).
My problem: I can plot the lines properly using leaflet, but I can't join the SpatialLines object to the base data to create a SpatialLinesDataFrame, and so I can't code the line color based on the speed var. I suspect the issue is that the IDs I'm assigning SL segments aren't matching to those present in the base df.
The objects I've tried to join, with SpatialLinesDataFrame():
"sl_object", a SpatialLines object with ~140 observations, one for each segment; I'm using Kyle's code, linked above, with one key change - instead of creating an arbitrary iterative ID value for each segment, I'm pulling the associated ID from my base data. (Or at least I'm trying to.) So, I've replaced:
id <- paste0("line", as.character(p))
with
lguy <- data.frame(paths[[p]][1])
id <- unique(lguy[,1])
"speed_object", a df with ~140 observations of a single speed var and row.names set to the same id var that I thought I created in the SL object above. (The number of observations will never exceed but may be smaller than the number of segments in the SL object.)
My joining code:
splndf <- SpatialLinesDataFrame(sl = sl_object, data = speed_object)
And the result:
row.names of data and Lines IDs do not match
Thanks, all. I'm posting this in part because I've seen some similar questions - including some referring specifically to changing the ID output of Kyle's great tool - and haven't been able to find a good answer.
EDIT: Including data samples.
From sl_obj, a single segment:
print(sl_obj)
Slot "ID":
[1] "4763655"
[[151]]
An object of class "Lines"
Slot "Lines":
[[1]]
An object of class "Line"
Slot "coords":
lon lat
1955 -74.05228 40.60397
1956 -74.05021 40.60465
1957 -74.04182 40.60737
1958 -74.03997 40.60795
1959 -74.03919 40.60821
And the corresponding record from speed_obj:
row.names speed
... ...
4763657 44.74
4763655 34.8 # this one matches the ID above
4616250 57.79
... ...
To get rid of this error message, either make the row.names of data and Lines IDs match by preparing sl_object and/or speed_object, or, in case you are certain that they should be matched in the order they appear, use
splndf <- SpatialLinesDataFrame(sl = sl_object, data = speed_object, match.ID = FALSE)
This is documented in ?SpatialLinesDataFrame.
All right, I figured it out. The error wasn't liking the fact that my speed_obj wasn't the same length as my sl_obj, as mentioned here. ("data =
object of class data.frame; the number of rows in data should equal the number of Lines elements in sl)
Resolution: used a quick loop to pull out all of the unique lines IDs, then performed a left join against that list of uniques to create an exhaustive speed_obj (with NAs, which seem to be OK).
ids <- data.frame()
for (i in (1:length(sl_obj))) {
id <- data.frame(sl_obj#lines[[i]]#ID)
ids <- rbind(ids, id)
}
colnames(ids)[1] <- "linkId"
speed_full <- join(ids, speed_obj)
speed_full_short <- data.frame(speed_obj[,c(-1)])
row.names(speed_full_short) <- speed_full$linkId
splndf <- SpatialLinesDataFrame(sl_obj, data = speed_full_short, match.ID = T)
Works fine now!
I may have deciphered the issue.
When I am pulling in my spatial lines data and I check the class it reads as
"Spatial Lines Data Frame" even though I know it's a simple linear shapefile, I'm using readOGR to bring the data in and I believe this is where the conversion is occurring. With that in mind the speed assignment is relatively easy.
sl_object$speed <- speed_object[ match( sl_object$ID , row.names( speed_object ) ) , "speed" ]
This should do the trick, as I'm willing to bet your class(sl_object) is "Spatial Lines Data Frame".
EDIT: I had received the same error as OP, driving me to check class()
I am under the impression that the error that was populated for you is because you were trying to coerce a data frame into a data frame and R wasn't a fan of that.