Dynamic variable names in for loops combined with gdal - r

I have the following problem trying to somehow solve:
I created 10 arrays of different IDW interpolations using the gstat/gdal package.
Now I have the following variables, which I´m trying to export to my harddrive:
DI.IDW.SAND.P.0.1, DI.IDW.SAND.P.0.2, [...]
In all it´s 10 different arrays for the different powers (ipd values) used during the interpolation.
Manual export to the hard drive works just fine:
writeGDAL(DI.IDW.Sand.P.0.1, fname = vec.rast[1], drivername = "GTiff", mvFlag = -1)
But now automation is where I can´t figure out the correct solution:
I created a data.frame containing the relevant information for the for loop:
runner.sand = data.frame("IDP" = c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1), "str"=c("DI.IDW.Sand.P.0.1", "DI.IDW.Sand.P.0.2", "DI.IDW.Sand.P.0.3", "DI.IDW.Sand.P.0.4", "DI.IDW.Sand.P.0.5", "DI.IDW.Sand.P.0.6", "DI.IDW.Sand.P.0.7", "DI.IDW.Sand.P.0.8", "DI.IDW.Sand.P.0.9", "DI.IDW.Sand.P.1"))
Then I tried:
for (i in c(1:length(runner.sand[,1]))) {
writeGDAL(paste("DI.IDW.Sand.P", runner.sand[i,1], sep = "."), runner.sand[i,2], drivername = "GTiff")
}
But I always get this error:
Error in nchar(fname) : 'nchar()' requires a character vector
Also when trying to automate proj4string I get this error:
for (i in c(1:length(runner.sand[,2]))) {
proj4string(runner.sand[i,2]) = CRS(paste("+init=epsg:",epsg,sep=""))
#dat.ov = over(IDP.opt, runner.sand[i,2]) # for later idp optimazation with residual values
#IDP.opt = cbind(IDP.opt#data, dat.ov)
}
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function 'proj4string' for signature '"character"'
Again, it perfectly works if I put in the "normal" variable name manually:
proj4string(DI.IDW.Sand.P.0.1) = CRS(paste("+init=epsg:",epsg,sep=""))
I also tried solutions shown here with assign, but nothing does the job.
Thanks for your help!

Related

Convert bed file to vcf with bed2vcf function

I am trying to convert .bed files to vcf by using the function bed2vcf from bedr R package.
I tried the following code:
cromXvcf <-
bed2vcf("cromXmerged2_pruned_removed_sex_mr_hh_sex_pop.bed",
filename = cromXmerged, zero.based = 1, header = NULL, fasta = "/media/iriel/Cosmos/Doctorado/Proyectos/Cromosoma X/Bases dedatos/human_g1k_v37.fasta")
and it throws the following error:
VALIDATE REGIONS * Checking input type... FAIL ERROR: Not sure what
the input format is! Error in is.valid.region(x) :
Can anybody tell what could be wrong? Any other suggestion of how could I do this conversion without using Perl?
I solved it by loading bed file to variable and changing datatypes for column 1 and 4.
Afterwards I also checked that my reference has chromosomes as chr1..22 not just 1...22.
The other thing I checked that my bed file is sorted.
x <- read.table("cromXmerged2_pruned_removed_sex_mr_hh_sex_pop.bed")
x$V1 <- as.character(x$V1)
x$V4 <- as.character(x$V4)
sapply(x, mode)
y <- bed2vcf(x, zero.based=True, header=NULL, fasta="/media/iriel/Cosmos/Doctorado/Proyectos/Cromosoma X/Bases dedatos/human_g1k_v37.fasta")
And it worked fine for me.

How can I create multiple shades in dygraphs using a data frame?

I have time series data that I plotted using dygraphs. Now I want to shade some events. The start and end points of these events are stored in a data frame. However, when I try to write a function to add multiple shades, I always get error messages.
I have tried using a for loop directly in the code, but then I get the error message that my dummy variable can't be found.
I have tried writing a function with a for loop, but somehow when I apply it, the first argument it uses is ".". Which of course messes up the function.
for (i in 1:length(dataframe$start)){
dyShading(from = dataframe$start[i], to = dataframe$end[i])
}
addshading <- function(periods){
for (i in 1:length(periods[,1])){
x <-dyShading(from = periods$start[i], to = periods$end[i])
}
x
}
Running the for loop directly after dygraph() %>% gives the following error message:
Error in function_list[k] : object 'i' not found
Running addshading(dataframe) directly after dygraph() %>% gives the following error message:
Error in addshading(., dataframe) : unused argument (dataframe)
I hope I made myself clear, I am new to ask for help with coding.
You need to assign dygraph() to an object first and then incrementally update that object with dyShading() -
p <- dygraph(your_data)
for (i in 1:nrow(dataframe)) {
p <- p %>% dyShading(from = dataframe$start[i], to = dataframe$end[i])
}
print(p)

Invalid 'path' argument with XLConnect

I am trying and failing to get the following process to complete in R Version 3.1.2:
library(RCurl)
library(XLConnect)
yr <- substr(Sys.Date(), 1, 4)
mo <- as.character(as.numeric(substr(Sys.Date(), 6, 7)) - 1)
temp <- tempfile()
temp <- getForm("http://strikemap.clb.org.hk/strikes/api.v4/export",
FromYear = "2011", FromMonth = "1",
ToYear = yr, ToMonth = mo,
`_lang` = "en")
CLB <- readWorksheetFromFile(temp, sheet=1)
unlink(temp)
I have been able manually to export the requested data set and then read it into R from a local directory using the same readWorksheetFromFile syntax. My goal now is to do the whole thing in R. The call to the API seems to work (thanks to some earlier help), but the process fails at the next step, when I try to ingest the results. Here's what happens:
> CLB <- readWorksheetFromFile(temp, sheet=1)
Error in path.expand(filename) : invalid 'path' argument
Any thoughts on what I'm doing wrong or what's broken?
Turns out the problem didn't lie with XLConnect at all. Based on Hadley's tip that I needed to save the results of my query to the API to a file before reading them back into R, I have managed (almost) to complete the process using the following code:
library(httr)
library(readxl)
yr <- substr(Sys.Date(), 1, 4)
mo <- as.character(as.numeric(substr(Sys.Date(), 6, 7)) - 1)
baseURL <- paste0("http://strikemap.clb.org.hk/strikes/api.v4/export?FromYear=2011&FromMonth=1&ToYear=", yr, "&ToMonth=", mo, "&_lang=en")
queryList <- parse_url(baseURL)
clb <- GET(build_url(queryList), write_disk("clb.temp.xlsx", overwrite=TRUE))
CLB <- read_excel("clb.temp.xlsx")
The object that creates, CLB, includes the desired data with one glitch: the dates in the first column are not being read properly. If I open "clb.temp.xlsx" in Excel, they show up as expected (e.g., 2015-06-30, or 6/30/2015 if I click on the cell). But read_excel() is reading them as numbers that don't track to those dates in an obvious way (e.g., 42185 for 2015-06-30). I tried fixing that by specifying that they were dates in the call to read_excel, but that produced a long string of warnings about expecting dates but getting those numbers.
If I use readWorkSheetFromFile() instead of read_excel at that last step, here's what happens:
> CLB <- readWorksheetFromFile("clb.temp.xlsx")
Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘readWorksheet’ for signature ‘"workbook", "missing"’
I will search for a solution to the problem using read_excel and will create a new question if I can't find one.

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.

RecordLinkage Package and RLBigDataLinkage-Class Objects

I am attempting to use R package RecordLinkage, and am using two articles by the package authors as usage guides, in addition to the package documentation.
I am using 2 large datasets (100k+ rows), which I hope to link, and so I am using those elements of the package which are built around S4 class RLBigDataLinkage.
I begin by running the following lines in R:
>library('RecordLinkage')
>data1 <- as.data.frame(#source)
>data2 <- as.data.frame(#source)
>rpairs <- RLBigDataLinkage(data1, data2, strcmp = 2:8, exclude = 9:10)
This works fine (though it takes some time), and writes the necessary .ff files to deal with the large data sets.
If I then try:
>rpairs <- epiWeights(rpairs)
Or:
>rpairs <- epiWeights(rpairs, e = 0.01, f = getFrequencies(rpairs))
Then when I run:
>summary(rpairs)
I get the error message:
Error in dbGetQuery(object#con, "select count(*) from data1") :
error in evaluating the argument 'conn' in selecting a method for function 'dbGetQuery': Error: no slot of name "con" for this object of class "RLBigDataLinkage"
If, on the other hand, I run:
>result <- epiClassify(rpairs, 0.5)
>getTable(result)
I get the error message:
Error in table.ff(object#data#pairs$is_match, object#prediction, useNA = "ifany") :
Only vmodes integer currently allowed - are you sure ... contains only factors or integers?
I'm clearly missing something about how these objects need to be handled. Does anyone have any experience with this package that sees my error? Thanks kindly.
when the type of 'rpairs' is 'RLBigDataLinkage' use print(rpairs) ,you will get the summary of rpairs.

Resources