I'm still quite new to R, so I am still learning a lot everyday. I made a list of raster files which I had cropped using the extent of a polygon. The polygon is divided in zones and from each zone I would like to know the mean value from each raster in the list. The extract function seemed the best function to do this, however when running it, it shows an error message. The code I've come up with so far is below.
> z_shapecropRastKE.list <- list()
> for (i in 1:length(shapecropRastKE.list)){
+ z_shapecropRastKE.list[[i]] <- extract(shapecropRastKE.list[[i]], shapeLVZ.list[[1]], fun="SUM", df=TRUE)
+ }
Error in fun(res[[i]], na.rm = na.rm) : could not find function "fun"
The shapeLVZ.list is just a list of two polygons corresponding to the two areas of interest. The first one is needed for this calculation.
I've already checked the needed packages and my R version is also the newest. If you want me to provide you guys with more background or more code just let me know, this is my first time posting something :)
Thanks in advance!
try fun= sum
without the double quotations
Related
I'm trying to run a block bootstrapping function on some time series data (monthly interest rates for ~15 years).
My data is in a csv file with no header, all comprising one column and going down by row.
I installed the package bootstrap because tsboot wouldn't work for me.
Here is my code:
testFile = read.csv("\\Users\\unori/sample_data.csv")
theta <- function(x){mean(x)}
results = bootstrap(testFile,100,theta)
It tells me there are at least 50 errors. All of them say "In mean.default(x) : argument is not numeric or logical: returning NA"
What to do? It runs when I use the example in the documentation. I think it must be how my data is stored/imported?
Thanks in advance.
Try to supply a working, minimal example that reproduces your problem! Check here to see how to make a minimal reproducible example.
The error messages tells you that the thing you want to calculate the mean of, is not a number! So R will just return NA.
Suggestions for debugging:
Does the object 'testFile' exist?
What is the output of
str(testFile)
This works for me:
library(bootstrap)
testFile <- cars[,1]
theta <- function(x){mean(x)}
results = bootstrap(testFile,100,theta)
library(discretization)
data("CO2")
disc<- mdlp(CO2[4])
I just need to discretize the 4th column of the data set provided. Then it is getting an Error in data[1, ] : incorrect number of dimensions error. Could you please help me to fix this.
I don't know if this is what you're going for, but 1) mdlp needs more than just one column of data, and 2) it also has trouble working with complex objects like CO2. Here is one way to make it execute:
CO2.df <- as.data.frame(CO2) # strips the extra info
mdlp(CO2.df[,4:5])
Possibly it's a stupid question (but be patient, I'm a beginner in R's word)... I'm working with ImpulseDE2, a package designed to RNAseq data analysis along different times (see article for more information).
The running function (runImpulseDE2) requires a matrix counts and a annotation data frame. I've created both but it appears this error message:
Error in checkCounts(matCountData, "matCountData"): ERROR: matCountData contains non-integer elements. Requires count data.
I have tried some solutions and nothing seems to work (and I've not found any solution in the Internet)...
as.matrix(data)
(data + 1) > and there isn't NAs nor zero values that originate this error ($ which(is.na(data)) and $ which(data < 1), but both results are integer(0))
as.numeric(data) > and appears another error: ERROR: [Rownames of matCountData] was not given as input.
I think that's something I'm not realizing, but I'm totally locked. Every tip will be welcome!
And here is the (silly) solution! This function seems not to accept float numbers... so applying a simple round is enough to solve this error.
Thanks for your help!
I scripted a simple for-loop to iterate over each row of a data set to calculate the distance between two coordinates. The code uses the 'geosphere' package and the 'distm' function which takes two sets of coordinates and returns the distance in meters (which I convert to miles by multiplying by 0.00062137).
Here is my loop:
##For loop to find distance in miles for each coordinate pair
miles <- 0
for (i in i:3303) {
miles[i] <- distm(x = c(clean.zips[i,4], clean.zips[i,3]), y = c(clean.zips[i,7], clean.zips[i,6]))[,1] * 0.00062137
}
However, when I run it I receive an error:
Error: object 'i' not found
The thing is, I've run this code before and it worked. Other times, I get this error. I'm not changing any code, it just seems to randomly work only some of the times. I feel the loop must be constructed correctly if it does what I want on occasion, but why would it only work sometimes?
OK, I'm not certain what justifies the down votes on this, but guess I apologize to whomever thought that necessary.
The issue seems to have just been starting the indexing with an actual numeric value like Zheyuan suggested (i.e. using '1:3303' rather than 'i:3303'). I feel like I've created loops before using 'i in i:xxx' without first defining 'i' but maybe not. Anyway, it's solved and thank you!
I'm on a project in remote sensing running on R. I've got a RasterBrick(x) with the raster for all the dates I'm interested in, a Time Serie with the dates corresponding (called time in the function), and a function which works as I want it when processed manually (z is the pixel I want) :
function(x,z)
{
d<-bfastts(as.vector(x[as.numeric(z)]),time,type="16-day")
n<-bfast(d, h=0.15, season="harmonic", max.iter = 1)
l[[z]]<-list(n$output[[1]]$Tt)
}
The bfastts function is used to create a ts object containing the values of one pixel along the time serie, the bfast is another processing some statisticals of which I only want one result (this is the third line)? None of this two functions are mine, and they are stable and foundable in the R package repository.
So, I would like to add "another level" of function (sorry for my vocabulary which may not be very precise) which would allow to run this function automatically. My expected result would be a list of the result of the function above, so in other words a list of each pixel's time series.
I've tried this (x is still the RasterBrick) :
function(x)
{
z<-nrow(x)*ncol(x)
j<-last(z[[1]])
l<-vector('list',length = j)
index<-function(x)
{
d<-bfastts(as.vector(x[as.numeric(z)]),time,type="16-day")
n<-bfast(d, h=0.15, season="harmonic", max.iter = 1)
l[[z]]<-list(n$output[[1]]$Tt) # this is to add the newly created element to the list
}
lapply(x, FUN='index')
}
but I'm getting an answer that it is not possible to coerce a S4 object to a vector, I guess the problem is in lapply who doesn't like the RasterBrick class... Furthermore I want a list of list in output, and not a list of RasterBrick (I think I understood lapply returns a list of object with the same class as x).
I've tried different workaround, none succesfully, which is not surprising giving my low level in programming, and this one seems to me the closest to what I need. I don't think I fully understand neither how lapply works nor the use of a function in a function.
Thank you very much if you can help me.
Cheers
Guillaume
So, in case it could be useful to someone, here is how I solved this problem (it seems rather very simple finally), the "brick" object is the RasterBrick:
pixelts<- as.list(as.data.frame(t(as.data.frame(brick))))