Inserting the Characters at specific position of the file - r

I have a file that I made for my model input. But I has some missing characters. I want to insert these characters at their position that model can read its
Its class i checked is that
file. class("outfile.txt")
[1] "character
So the file from my codes after different codes is
HYDRUS_Version=5
WaterFlow=0
SoluteTransport=0
Unsatchem=0
Unsatchem=0
HP1=0
HeatTransport=0
EquilibriumAdsorption=0
MobileImmobile=0
RootWaterUptake=0
RootGrowth=0
MaterialNumbers=0
SubregionNumbers=0
SpaceUnit=cm
TimeUnit=days
PrintTimes=160
NumberOfSolutes=0
InitialCondition=1
NumberOfNodes=101
ProfileDepth=120
ObservationNodes=160
GridVisible=160
SnapToGrid=160
ProfileWidth=160
LeftMargin=160
GridOrgX=160
GridOrgY=160
GridDX=160
GridDY=160
and my required file is [I want to insert 4 characters in it, two ; and [Main] are at the start and two ; and [Profile] are inside the file. I want the final file as given below.
;
[Main] `
HYDRUS_Version=5
WaterFlow=0
SoluteTransport=0
Unsatchem=0
Unsatchem=0
HP1=0
HeatTransport=0
EquilibriumAdsorption=0
MobileImmobile=0
RootWaterUptake=0
RootGrowth=0
MaterialNumbers=0
SubregionNumbers=0
SpaceUnit=cm
TimeUnit=days
PrintTimes=160
NumberOfSolutes=0
InitialCondition=1
;
[Profile]
NumberOfNodes=101
ProfileDepth=120
ObservationNodes=160
GridVisible=160
SnapToGrid=160
ProfileWidth=160
LeftMargin=160
GridOrgX=160
GridOrgY=160
GridDX=160
GridDY=160

you can try:
imported_text <- readLines("outfile.txt")
[1] "HYDRUS_Version=5" "WaterFlow=0" "SoluteTransport=0" "Unsatchem=0" "Unsatchem=0" "HP1=0"
[7] "HeatTransport=0" "EquilibriumAdsorption=0" "MobileImmobile=0" "RootWaterUptake=0" "RootGrowth=0" "MaterialNumbers=0"
[13] "SubregionNumbers=0" "SpaceUnit=cm" "TimeUnit=days" "PrintTimes=160" "NumberOfSolutes=0" "InitialCondition=1"
[19] "NumberOfNodes=101" "ProfileDepth=120" "ObservationNodes=160" "GridVisible=160" "SnapToGrid=160" "ProfileWidth=160"
[25] "LeftMargin=160" "GridOrgX=160" "GridOrgY=160" "GridDX=160" "GridDY=160"
and try the c function:
imported_text2 <- c("; `here I want this`","[Main] `here I want this`",imported_text)
Furthermore you can do: select the
final_text <- c(imported_text2[1:20],"; `here I want this`","[Profile] `here I want this`",imported_text2[21:length(imported_text2)])
[1] "; `here I want this`" "[Main] `here I want this`" "HYDRUS_Version=5"
[4] "WaterFlow=0" "SoluteTransport=0" "Unsatchem=0"
[7] "Unsatchem=0" "HP1=0" "HeatTransport=0"
[10] "EquilibriumAdsorption=0" "MobileImmobile=0" "RootWaterUptake=0"
[13] "RootGrowth=0" "MaterialNumbers=0" "SubregionNumbers=0"
[16] "SpaceUnit=cm" "TimeUnit=days" "PrintTimes=160"
[19] "NumberOfSolutes=0" "InitialCondition=1" "; `here I want this`"
[22] "[Profile] `here I want this`" "NumberOfNodes=101" "ProfileDepth=120"
[25] "ObservationNodes=160" "GridVisible=160" "SnapToGrid=160"
[28] "ProfileWidth=160" "LeftMargin=160" "GridOrgX=160"
[31] "GridOrgY=160" "GridDX=160" "GridDY=160"
EDIT
In order to have the new output (as per OP request):
c(";","[Main]",text[1:18],";","[Profile]",text[19:length(text)])

Related

Building CPLEX fails in Julia

I am trying to install CPLEX in Julia on a computer cluster. I got the following error when I did Pkg.build("CPLEX"). It seems that I didn't pass the right path. I know how to correct it on my local computer but could anyone tell me how to fix it on a computer cluster? Or is it because of some other problems?
For your information,
CPLEX version: 12.10-GCCcore-8.3.0,
Julia version: 1.7.1.
Thanks very much in advance.
Hongyu.
The versions of CPLEX supported by CPLEX.jl are:
* 12.10
* 20.1
You must download and install one of these versions separately.
You should set the `CPLEX_STUDIO_BINARIES` environment variable to point to
the install location then try again. For example (updating the path to the
correct location):
ENV["CPLEX_STUDIO_BINARIES"] = "/opt/CPLEX_Studio201/cplex/bin/x86-64_linux/"
import Pkg
Pkg.add("CPLEX")
Pkg.build("CPLEX")
See the CPLEX.jl README at https://github.com/jump-dev/CPLEX.jl for further
instructions.
Stacktrace:
[1] error(s::String)
# Base ./error.jl:33
[2] try_local_installation()
# Main ~/.julia/packages/CPLEX/88lum/deps/build.jl:132
[3] top-level scope
# ~/.julia/packages/CPLEX/88lum/deps/build.jl:150
[4] include(fname::String)
# Base.MainInclude ./client.jl:451
[5] top-level scope
# none:5
in expression starting at /home/hongyuzh/.julia/packages/CPLEX/88lum/deps/build.jl:143
Stacktrace:
[1] pkgerror(msg::String)
# Pkg.Types /share/apps/Julia/1.7.1-linux-x86_64/share/julia/stdlib/v1.7/Pkg/src/Types.jl:68
[2] (::Pkg.Operations.var"#62#67"{Bool, Pkg.Types.Context, String, Pkg.Types.PackageSpec})()
# Pkg.Operations /share/apps/Julia/1.7.1-linux-x86_64/share/julia/stdlib/v1.7/Pkg/src/Operations.jl:962
[3] withenv(::Pkg.Operations.var"#62#67"{Bool, Pkg.Types.Context, String, Pkg.Types.PackageSpec}, ::Pair{String, String}, ::Vararg{Pair{String}})
# Base ./env.jl:172
[4] (::Pkg.Operations.var"#99#103"{String, Bool, Bool, Bool, Pkg.Operations.var"#62#67"{Bool, Pkg.Types.Context, String, Pkg.Types.PackageSpec}, Pkg.Types.PackageSpec})()
# Pkg.Operations /share/apps/Julia/1.7.1-linux-x86_64/share/julia/stdlib/v1.7/Pkg/src/Operations.jl:1506
[5] with_temp_env(fn::Pkg.Operations.var"#99#103"{String, Bool, Bool, Bool, Pkg.Operations.var"#62#67"{Bool, Pkg.Types.Context, String, Pkg.Types.PackageSpec}, Pkg.Types.PackageSpec}, temp_env::String)
# Pkg.Operations /share/apps/Julia/1.7.1-linux-x86_64/share/julia/stdlib/v1.7/Pkg/src/Operations.jl:1390
[6] (::Pkg.Operations.var"#98#102"{Bool, Bool, Bool, Pkg.Operations.var"#62#67"{Bool, Pkg.Types.Context, String, Pkg.Types.PackageSpec}, Pkg.Types.Context, Pkg.Types.PackageSpec, String, Pkg.Types.Project, String})(tmp::String)
# Pkg.Operations /share/apps/Julia/1.7.1-linux-x86_64/share/julia/stdlib/v1.7/Pkg/src/Operations.jl:1469
[7] mktempdir(fn::Pkg.Operations.var"#98#102"{Bool, Bool, Bool, Pkg.Operations.var"#62#67"{Bool, Pkg.Types.Context, String, Pkg.Types.PackageSpec}, Pkg.Types.Context, Pkg.Types.PackageSpec, String, Pkg.Types.Project, String}, parent::String; prefix::String)
# Base.Filesystem ./file.jl:750
[8] mktempdir(fn::Function, parent::String) (repeats 2 times)
# Base.Filesystem ./file.jl:748
[9] sandbox(fn::Function, ctx::Pkg.Types.Context, target::Pkg.Types.PackageSpec, target_path::String, sandbox_path::String, sandbox_project_override::Pkg.Types.Project; force_latest_compatible_version::Bool, allow_earlier_backwards_compatible_versions::Bool, allow_reresolve::Bool)
# Pkg.Operations /share/apps/Julia/1.7.1-linux-x86_64/share/julia/stdlib/v1.7/Pkg/src/Operations.jl:1435
[10] sandbox(fn::Function, ctx::Pkg.Types.Context, target::Pkg.Types.PackageSpec, target_path::String, sandbox_path::String, sandbox_project_override::Pkg.Types.Project)
# Pkg.Operations /share/apps/Julia/1.7.1-linux-x86_64/share/julia/stdlib/v1.7/Pkg/src/Operations.jl:1432
[11] build_versions(ctx::Pkg.Types.Context, uuids::Set{Base.UUID}; verbose::Bool)
# Pkg.Operations /share/apps/Julia/1.7.1-linux-x86_64/share/julia/stdlib/v1.7/Pkg/src/Operations.jl:943
[12] build(ctx::Pkg.Types.Context, uuids::Set{Base.UUID}, verbose::Bool)
# Pkg.Operations /share/apps/Julia/1.7.1-linux-x86_64/share/julia/stdlib/v1.7/Pkg/src/Operations.jl:822
[13] build(ctx::Pkg.Types.Context, pkgs::Vector{Pkg.Types.PackageSpec}; verbose::Bool, kwargs::Base.Pairs{Symbol, Base.TTY, Tuple{Symbol}, NamedTuple{(:io,), Tuple{Base.TTY}}})
# Pkg.API /share/apps/Julia/1.7.1-linux-x86_64/share/julia/stdlib/v1.7/Pkg/src/API.jl:992
[14] build(pkgs::Vector{Pkg.Types.PackageSpec}; io::Base.TTY, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
# Pkg.API /share/apps/Julia/1.7.1-linux-x86_64/share/julia/stdlib/v1.7/Pkg/src/API.jl:149
[15] build(pkgs::Vector{Pkg.Types.PackageSpec})
# Pkg.API /share/apps/Julia/1.7.1-linux-x86_64/share/julia/stdlib/v1.7/Pkg/src/API.jl:144
[16] #build#99
# /share/apps/Julia/1.7.1-linux-x86_64/share/julia/stdlib/v1.7/Pkg/src/API.jl:142 [inlined]
[17] build
# /share/apps/Julia/1.7.1-linux-x86_64/share/julia/stdlib/v1.7/Pkg/src/API.jl:142 [inlined]
[18] #build#98
# /share/apps/Julia/1.7.1-linux-x86_64/share/julia/stdlib/v1.7/Pkg/src/API.jl:141 [inlined]
[19] build(pkg::String)
# Pkg.API /share/apps/Julia/1.7.1-linux-x86_64/share/julia/stdlib/v1.7/Pkg/src/API.jl:141
[20] top-level scope
# REPL[138]:1
(Solution found in comment discussion, adding as answer for better visibility.)
As the error message indicates, you have to set the ENV["CPLEX_STUDIO_BINARIES"] value to the directory that contains cplex. You can find that by running which cplex | xargs dirname (or simply which cplex and removing the final cplex from the result yourself).
One you set CPLEX_STUDIO_BINARIES to that path, you can run Pkg.build("CPLEX") again to let CPLEX.jl pick up the binaries from the path.

Return level in R

I am trying to compute return levels for a certain return period (20 years) for my extreme precipitation. Using the block maxima approach, I made the following object to extract the maximum values:
Gcomb <-c(Gmax[1], Gmax1[1], Gmax2[1], Gmax3[1], Gmax4[1], Gmax5[1], Gmax6[1], Gmax7[1], Gmax8[1],
Gmax[2], Gmax1[2], Gmax2[2], Gmax3[2], Gmax4[2], Gmax5[2], Gmax6[2], Gmax7[2], Gmax8[2], Gmax[3],
Gmax1[3], Gmax2[3], Gmax3[3], Gmax4[3], Gmax5[3], Gmax6[3], Gmax7[3], Gmax8[3], Gmax[4], Gmax1[4],
Gmax2[4], Gmax3[4], Gmax4[4], Gmax5[4], Gmax6[4], Gmax7[4], Gmax8[4], Gmax[5], Gmax1[5], Gmax2[5],
Gmax3[5], Gmax4[5], Gmax5[5], Gmax6[5], Gmax7[5], Gmax8[5], Gmax[6], Gmax1[6], Gmax2[6], Gmax3[6],
Gmax4[6], Gmax5[6], Gmax6[6], Gmax7[6], Gmax8[6], Gmax[7], Gmax1[7], Gmax2[7], Gmax3[7], Gmax4[7],
Gmax5[7], Gmax6[7], Gmax7[7], Gmax8[7])
Gcomb looks like this:
>Gcomb
[1] 191.46960 115.77133 209.15147 243.26880 84.97396 157.38678 138.84937
140.96840 143.80301 196.87990 131.89099
[12] 137.08264 231.11342 116.67323 125.27563 100.72293 159.45056 198.12399
216.74820 120.16774 147.73828 396.64862
[23] 164.02292 132.94519 99.95055 162.81708 186.59691 219.53568 124.76086
174.99396 231.20859 108.70350 139.22649
[34] 160.40935 159.03806 162.56891 217.58543 123.11100 149.26378 245.90994
92.68985 152.45033 171.12381 159.27040
[45] 141.19446 205.46706 101.71022 163.25592 328.31265 119.69154 122.01060
114.79624 147.92126 179.91318 200.70750
[56] 110.50108 231.30966 213.02881 125.74141 152.19133 98.35700 204.43138
182.53252
Now, when I try to compute the return level (a 20-year return level):
library(extRemes)
return.level.fevd(Gcomb, return.period = 20)
I end up with the following error:
Error: $ operator is invalid for atomic vectors
What could be causing this error? I tried creating a vector with the same values, but the error persists
Any help or insight for this would be very appreciated!

Why does Julia throw a premature end of input error when i write and if statement?

This is my code:
using Printf
using Statistics
age = 12
if age < 10
println("$age")
This is the error:
ERROR: LoadError: syntax: incomplete: premature end of input
Stacktrace:
[1] top-level scope at D:\julia\trial.jl:5
[2] include(::Module, ::String) at .\Base.jl:377
[3] exec_options(::Base.JLOptions) at .\client.jl:288
[4] _start() at .\client.jl:484
in expression starting at D:\julia\trial.jl:5
It works well if I remove the if statement and just print the value of age.
Julia terminates its blocks with an end keyword (and does not rely on whitespace to define its blocks).
age = 12
if age < 10
println("$age")
end

How to keep non-alphanumeric symbols when tokenizing words in R?

I am using the tokenizers package in R for tokenizing a text, but non-alphanumeric symbols such as "#" or "&" are lost and I need to keep them. Here is the function I am using:
tokenize_ngrams("My number & email address user#website.com", lowercase = FALSE, n = 3, n_min = 1,stopwords = character(), ngram_delim = " ", simplify = FALSE)
I know tokenize_character_shingles has the strip_non_alphanum argument that allows keeping the punctuation, but the tokenization is applied to characters, not words.
Anyone knows how to handle this issue?
If you are okay to use a different package ngram, this has two useful functions that retains those non-alpha
> library(ngram)
> print(ngram("My number & email address user#website.com",n = 2), output = 'full')
number & | 1
email {1} |
My number | 1
& {1} |
address user#website.com | 1
NULL {1} |
& email | 1
address {1} |
email address | 1
user#website.com {1} |
> print(ngram_asweka("My number & email address user#website.com",1,3), output = 'full')
[1] "My number &" "number & email"
[3] "& email address" "email address user#website.com"
[5] "My number" "number &"
[7] "& email" "email address"
[9] "address user#website.com" "My"
[11] "number" "&"
[13] "email" "address"
[15] "user#website.com"
>
Another beautiful package quanteda gives more flexibility with remove_punct paramater.
> library(quanteda)
> tokenize(text, ngrams = 1:3)
tokenizedTexts from 1 document.
Component 1 :
[1] "My" "number"
[3] "&" "email"
[5] "address" "user#website.com"
[7] "My_number" "number_&"
[9] "&_email" "email_address"
[11] "address_user#website.com" "My_number_&"
[13] "number_&_email" "&_email_address"
[15] "email_address_user#website.com"
>

Using R package ggmap with Google Directions API token

I'm using ggmap's "route" function to plot driving directions using Google's Driving Directions API. I may need to exceed the 2,500 daily API limit. I know that to exceed that threshold, I'll have to pay the $0.50/1000 hits fee and sign up for a Google Developers API token. However, I don't see any parameters in the ggmap library or route function that allow me to enter my token and key information so that I can exceed the threshold. What am I missing?
I've written the package googleway to access google maps API where you can specify your token key
For example
library(googlway)
key <- "your_api_key"
google_directions(origin = "MCG, Melbourne",
destination = "Flinders Street Station, Melbourne",
key = key,
simplify = F) ## use simplify = T to return a data.frame
[1] "{"
[2] " \"geocoded_waypoints\" : ["
[3] " {"
[4] " \"geocoder_status\" : \"OK\","
[5] " \"partial_match\" : true,"
[6] " \"place_id\" : \"ChIJIdtrbupC1moRMPT0CXZWBB0\","
[7] " \"types\" : ["
[8] " \"establishment\","
[9] " \"point_of_interest\","
[10] " \"train_station\","
[11] " \"transit_station\""
[12] " ]"
[13] " },"
[14] " {"
[15] " \"geocoder_status\" : \"OK\","
[16] " \"place_id\" : \"ChIJSSKDr7ZC1moRTsSnSV5BnuM\","
[17] " \"types\" : ["
[18] " \"establishment\","
[19] " \"point_of_interest\","
[20] " \"train_station\","
[21] " \"transit_station\""
[22] " ]"
[23] " }"
[24] " ],"
[25] " \"routes\" : ["
... etc

Resources