Error in if (xn == xx) { : missing value where TRUE/FALSE needed - r

I'm trying to combine a large number of raster tiles to a single mosaic using R codes as follows. The error that appears is:
Error in if (xn == xx) { : missing value where TRUE/FALSE needed
The error appears after the for loop.
I will highly appreciate your suggestion.
require(raster)
rasters1 <- list.files("D:/lidar_grid_metrics/ElevMax",
pattern="*.asc$", full.names=TRUE, recursive=TRUE)
rast.list <- list()
for(i in 1:length(rasters1)) { rast.list[i] <- raster(rasters1[i]) }
rast.list$fun <- mean
rast.mosaic <- do.call(mosaic,rast.list)
plot(rast.mosaic)

First a better way to write what you do (use lapply)
library(raster)
ff <- list.files("D:/lidar_grid_metrics/ElevMax",
pattern="\\.asc$", full.names=TRUE, recursive=TRUE)
rast.list <- lapply(ff, raster)
rast.list$fun <- mean
rast.mosaic <- do.call(mosaic,rast.list)
Now, to the error your get. It is useful to show the results of traceback() after the error occurs. But from the error message you get, I infer that one of the RasterLayers has an extent with an NA value. That makes it invalid. You can check if that is true (and if so figure out what is going on) by doing
t(sapply(rast.list, function(i) as.vector(extent(i))))
EDIT
With the files Ram send me I figured out what was going on. There was a bug when creating a RasterLayer from an ascii file with the native driver if the file specifies "xllcenter" rather than "xllcorner".
This is now fixed on the development version (2.9-1) available on github.
The problem can also be avoided by installing rgdal because if rgdal is available, the native driver won't be used.

Related

Creating a compartive object in R from two dataframes for comparitive phylogenetics

I'm trying to read in two dataframes into a comparitive object so I can plot them using pgls.
I'm not sure what the error being returned means, and how to go about getting rid of it.
My code:
library(ape)
library(geiger)
library(caper)
taxatree <- read.nexus("taxonomyforzeldospecies.nex")
LWEVIYRcombodata <- read.csv("LWEVIYR.csv")
LWEVIYRcombodataPGLS <-data.frame(LWEVIYRcombodata$Sum.of.percentage,OGT=LWEVIYRcombodata$OGT, Species=LWEVIYRcombodata$Species)
comp.dat <- comparative.data(taxatree, LWEVIYRcombodataPGLS, "Species")
Returns error:
> comp.dat <- comparative.data(taxatree, LWEVIYRcombodataPGLS, 'Species')
Error in if (tabulate(phy$edge[, 1])[ntips + 1] > 2) FALSE else TRUE :
missing value where TRUE/FALSE needed
This might come from your data set and your phylogeny having some discrepancies that comparative.data struggles to handle (by the look of the error message).
You can try cleaning both the data set and the tree using dispRity::clean.data:
library(dispRity)
## Reading the data
taxatree <- read.nexus("taxonomyforzeldospecies.nex")
LWEVIYRcombodata <- read.csv("LWEVIYR.csv")
LWEVIYRcombodataPGLS <- data.frame(LWEVIYRcombodata$Sum.of.percentage,OGT=LWEVIYRcombodata$OGT, Species=LWEVIYRcombodata$Species)
## Cleaning the data
cleaned_data <- clean.data(LWEVIYRcombodataPGLS, taxatree)
## Preparing the comparative data object
comp.dat <- comparative.data(cleaned_data$tree, cleaned_data$data, "Species")
However, as #MrFlick suggests, it's hard to know if that solves the problem without a reproducible example.
The error here is that I was using a nexus file, although ?comparitive.data does not specify which phylo objects it should use, newick trees seem to work fine, whereas nexus files do not.

Skip error in lapply and continue processing ncdf4 files in R

I submitted an R script on LINUX HPC using the "sub" script. I have written a functionin R to apply to a list. However, it stops running once it encounters a bad file. How do I write the R function in such a way that it skips the error and continues on the good netcdf files? the script:
##list files in the SEVIRI data folder
LST1<-list.files(pattern="GT_SSD.*\\.nc",recursive=T, path="/data atsr/SEVIRI/2007")
##Function to create rasters
fun2<-function(x){
##Open the files
y1<-nc_open(x)
##Get soil moisture variable
y2<-ncvar_get( y1,"LST")
y3<-t(y2)
R1<-raster(y3, xmn=-80,xmx=80,ymn=-42,ymx=80)
proj4string(R1)<-CRS("+proj=longlat +ellps=WGS84")
frm <- extent(c(-19, 19,2,29))
pfrm <- as(frm, 'SpatialPolygons')
R3<-crop(R1,pfrm)}
When I apply the function
LST2<-lapply(LST1,fun2)
The error message is:
Error in nc_open(x) :
Error in nc_open trying to open file GT_SEV_2P/GT_SSD- L2-SEVIR_LST_2-20110122_010000-LIPM-0.05X0.05-V1.0.nc
The script stops running once this happen. How do I ensure it keeps running on the good ones, please? The codes above are just the first set of codes.
Here is an example with try. Note that I much simplified your function. I cannot be sure about this, as I do not have your data, but this much more direct approach works in most cases. You certainly do not need to create a SpatialPolygons object for use in crop.
fun2 <- function(x, ext) {
R1 <- try(raster(x, var="LST"), silent=TRUE)
if (class(R1) == 'try-error') {
return(NA)
}
frm <- extent(c(-19, 19, 2, 29))
crop(R1, frm)
}
x <- lapply(LST1, fun2)

How to include bootstrap values in phylogenetic trees in ape package

I am using the R package ape to analyze some sequences stored in a DNAbin object:
library(ape)
my.seq <- read.dna("sequences.txt", format = "clustal")
my.dist <- dist.dna(my.seq)
my.tree <- nj(my.dist)
I want to find the bootstrap values, so I use boot.phylo:
boot <- boot.phylo(my.tree, my.seq, FUN = function(xx) nj(dist.dna(xx)), B = 100)
But I get an error message saying:
Error in if (drop[j]) next : missing value where TRUE/FALSE needed
Any idea what this means, and how to fix it? I tried googling the error message, and I could not find anything.
Your if condition resulted in an NA.
It must have either TRUE or FALSEresult.

Combining vectors of unequal size using rbind.na

I've imported some data files with an unequal number of columns and was hoping to create a data frame out of them. I've use lapply to convert them into vectors, and now I'm trying to put these vectors into a data frame.
I'm using rbind.na from the package {qpcR} to try out and fill out the remaining elements of each vector with NA so they all become the same size. For some reason the function isn't being recognized by do.call. Can anyone figure out why this is the case?
library(plyr)
library(qpcR)
files <- list.files(path = "C:/documents", pattern = "*.txt", full.names = TRUE)
readdata <- function(x)
{
con <- file(x, open="rt")
mydata <- readLines(con, warn = FALSE, encoding = "UTF-8")
close(con)
return(mydata)
}
all.files <- lapply(files, readdata)
combine <- do.call(rbind.na, all.files)
If anyone has any potential alternatives they can think of I'm open to that too. I actually tried using a function from here but my output didn't give me any columns.
Here is the error:
Error in do.call(rbind.na, all.files) : object 'rbind.na' not found
The package has definitely been installed too.
EDIT: changed cbind.na to rbind.na for the error.
It appears that the function is not exported by the package. Using qpcR:::rbind.na will allow you to access the function.
The triple colon allows you to access the internal variables of a namespace. Be aware though that ?":::" advises against using it in your code, presumably because objects that aren't exported can't be relied upon in future versions of a package. It suggests contacting the package maintainer to export the object if it is stable and useful.

non-numeric argument error while running FFT on a matrix

I read eachline of a csv file and save the first element of each line in a list, then I want to run FFT on this list, but I get this error:
Error in fft(x) : non-numeric argument
in my Example hier I read 4 rows:
con<-file("C:\\bla\\test.csv","r")
datalist<-list()
m<-list()
for(i in 1:4)
{
line<-readLines(con,n=1,warn=FALSE)
m<-list(as.integer(unlist(strsplit(line,split=","))))
datalist<-c(datalist,sapply(m,"[[",1))
}
datalist
close(con)
fftfun<- function(x) {fft(x)}
fft_amplitude <- function(x) {sqrt((Re(fft(x)))^2+(Im(fft(x)))^2)} }
apply(as.matrix(datalist),2,FUN=fftfun)
what should I do to solve this problem?
EDIT
My rows in csv file:
12,85,365,145,23
13,84,364,144,21
14,86,366,143,24
15,83,363,146,22
16,85,365,145,23
17,80,361,142,21
Your code seems overly complicated. Why don't you just do something like this :
df <- read.csv("test.csv", header=FALSE)
x <- df[,1]
fft(x)
Or, if you really want to read line by line :
con <- file("test.csv","r")
data <- NULL
for (i in 1:4) {
line<-readLines(con,n=1,warn=FALSE)
data <- c(data, as.numeric(strsplit(line,split=",")[[1]][1]))
}
close(con)
fft(data)
Let's assume your real question is: what happened to make apparently numeric data become non-numeric? Rather than slogging through the incredible number of type coercions in your code (csv to matrix to list to other list to as.matrix), I'm going to recommend you start by just plain reading one file into R and checking the typeof and class of each column. If anything turns out to be a factor rather than numeric , you may need to add the argument colClasses='character' .
If the data as read are numeric, then you're fouling it up in your subsequent conversions. Try simplifying the code as much as possible.

Resources