Tag Archives: r language

Solution of Prophet error reporting when installing R package in Kubuntu virtual machine environment

Initial error reporting environment

Oracle VM VirtualBox 6.1.22
kunbuntu-21.04
memory 2048MB
storage space 20g

Error reporting and Solutions

If I directly install. Packages (‘prophet ‘) in R according to the above environment, three environment dependent errors will be reported, namely libcurl, libdev and rstan
first solve the problem that libcurl and libdev are not installed. First q() exit the R environment and install the two development tools:

sudo apt-get install libcurl4-openssl-dev
sudo apt-get install libv8-dev

Then enter the R environment and reinstall rstan. The key errors are as follows:

g++: internal compiler error: Killed (program cc1plus)
ERROR: compilation failed for package 'rstan'
In install.packages("rstan") :
  installation of package 'rstan' had non-zero exit status

There are many similar error reports and strange solutions on the Internet. In fact, most cases are caused by insufficient memory allocation. You can adjust the allocated memory to 4096mb in the virtual machine. After installing the above dependencies, reinstall. Packages (‘prophet ‘) and the installation will succeed.

The solution of “error in NLS loop more than 50” in R language

When using multiple nonlinear regression (NLS function) in R language, we often encounter the problem that “the number of error in NLS cycles exceeds the maximum of 50”.

The main reason is that the default maximum number of iterations in NLS is 50. At this time, you only need to use NLS. Control to modify the maximum number of iterations
for example, change the maximum number of iterations to 1000:

nlc <- nls.control(maxiter = 1000)
m1 <- nls(y ~ a * x1 ^ b * x2 ^ c, 
          control = nlc, 
          start = list(a = 1, b = 1, c = 1),
          trace = T)

R language notes – sample() function

Field studies and sample selection in medical statistics or epidemiology often use one word: random sampling. Random sampling is an important method to ensure the equilibrium among comparison groups. So the first function introduced today is the function sample for sampling:

> x=1:10
> sample(x=x)

 [1]  3  5  9  6 10  7  2  1  8  4

The first line represents assigning the x vector 1 to 10, and the second line represents random sampling of the x vector. The output is the result of each sampling, and it can be seen that the sampling is not put back — at most n times, n is the number of elements in the x vector.

if you want to specify the number of elements extracted from the vector, you need to add a parameter size:

> x=1:1000
> sample(x=x,size=20)

 [1]  66 891 606 924 871 374 879 573 284 305 914 792 398 497 721 897 324 437
[19] 901  33

This is sampled in positive integers from 1 to 1000, where size specifies the number of times the sample is sampled, 20 times, and the result is shown above.
These are not put back into the sample. No put back sampling means that once an element is selected, there will be no more of that element in the population. If the sample is put back, a parameter repalce=T should be added:

> x=1:10
> sample(x=x,size=5,replace=T)

[1] 4 7 2 4 8

“Replace” means to repeat. So you can sample the elements repeatedly, which is what’s called a put back sample. We look at the results above. Element 4 is selected twice in the course of 5 random sampling.


R language code has a feature is “contraption”, maybe my word is not professional, but it means: if we enter the position of the code corresponds to the position of the parameters in a function, we can not write the parameters of the function, such as:

> x=1:10
> sample(x,20,T)

 [1] 1 2 2 1 5 5 5 9 9 5 2 9 8 3 4 8 8 8 1 1

In the above code, we have omitted the parameters x, size and Repalce, but it can still be evaluated and indicates that the x vector is put back to random extraction 20 times. The reason we try to take parameters with us every time we write code is because I think it’s a good habit and it looks clear. In addition, if you are familiar with the location of a function’s arguments, you will get the wrong result if there is no “counterpoint”. And many functions have too many arguments to remember where they are. If the parameters are taken, the operation can be carried out even if the positions do not correspond:

> x=1:10
> sample(size=20,replace=T,x=x)

 [1]  4  9  2  6  4  5  4  7 10  5  2  2  3  4  2  4  6  8  7  8

This advantage is obvious, not only clear, but also has no counterpart. And we can also see that if you put it back, the size is infinite, and if you don’t put it back, the size depends on the size of the population.

for the roll of dice, the roll of a coin (this is probably a necessary introduction to sampling), is a put back sampling.
It should be explained here that for the SAMPLE function, the parameter x can be either a numerical value or a character. In fact, the parameter x represents any vector:

> a=c("A","B")
> sample(x=a,size=10,replace=T)

 [1] "B" "A" "A" "A" "B" "A" "A" "B" "A" "A"

The code above can be interpreted as the number of flips of A coin, in which heads (A) and tails (B) occur 10 times.

above mentioned sampling process, each element is drawn with equal probability, called random sampling.
Sometimes our probability of extracting elements may not be equal (for example, common binomial distribution probability problems). In this case, we need to add a parameter prob, which is the abbreviation of “probability”. If a doctor performs an operation on a patient with an 80% chance of success, how many times can he operate on 20 patients today?The code is as follows:

> x=c("S","F")
> sample(x,size=20,replace=T,prob=c(0.8,0.2))

 [1] "F" "S" "S" "S" "S" "S" "S" "S" "S" "S" "S" "S" "F" "S" "S" "F" "S" "S"
[19] "F" "S"

Where “S” stands for success and “F” for failure.

> x=c(1,3,5,7)
> sample(x,size=20,replace=T,prob=c(0.1,0.2,0.3,0.9))

 [1] 3 5 7 3 7 3 7 5 3 7 7 7 1 5 7 5 7 7 3 7

These codes tell us that each element can be given a probability, and each probability is independent, that is, in the parameter PROb, the probability of all elements does not necessarily add up to 1, it only represents the probability of an element being extracted.

for the sample function, the parameter x can be any object in R (such as the sample character above). Another of the same functions is sample.int, short for “intger” or “integer.” Its argument n must be a positive integer:

> x=-10.5:7.5
> sample(x=x,size=3);sample.int(n=x,size=3)

[1] -5.5 -7.5  0.5
Error in sample.int(x, size = 3) : invalid first argument

The first line of code generates an arithmetic sequence of -10.5 to 7.5. The first line of output is the result of SAMPLE. The second line is the result of sample.int with an error: “First argument invalid” because it is not a positive integer. The rest of the usage is the same as sample.

pick from http://www.wtoutiao.com/p/186VWin.html

R language: na.fail and na.omit

In practice, the data set is rarely complete, and in many cases, the sample will contain several missing values NA, which is troublesome in data analysis and mining.
R language can improve the omit value of samples by na.fail and na.omit.
The

    na. Fail (& lt; Vector a>) : If vector A contains at least 1 NA, error is returned; If NA is excluded, return the original vector ana. Omit (& LT; Vector a>) : Return the vector aattr (na.omit (& LT); Vector a>) , “na.action”) : returns the subscript of na in vector a. Na: determines whether the element in the vector is na

Example:

data< – c (1, 2, NA, 2,4,2,10, NA, 9)
data. NA. Omit< – na. Omit (data)
data. Na. Omit the
[1] 1 2 2 and 4 2 10 9
attr (, “na. The action”)
3 8 [1]
attr (” class “)
[1] “omit”

attr (data. Na. Omit, “na. The action”)
3 8 [1]
attr (” class “)
[1] “omit”

can also be used! X mode conveniently deletes NA. Such as:

a< – c (1, 2, 3, NA, NA, 2, NA, 5)
a[!is.na(a)]
[1] 1 2 3 2 5

.
which is for na na is used to determine whether the element in the vector, returns the result: c (FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE), namely the elements within a as na, its corresponding subscript elements is TRUE, otherwise is FALSE. ! X is the non-logical operator,! Is. Na (a) means that the element inside a is not Na, and its subscript element is TRUE and FALSE conversely. After indexing through A [! Is.na (a)], the element that is not Na in A can be taken out and filtered.
The functions Na. fail and Na. omit can be applied not only to vectors but also to matrices and data boxes.
Example:

data < – read.table(text=”
a b c d e f
NA 1 1 1 1 1
1 NA 1 1 1 1
1 1 NA 1 1 1
1 1 1 NA 1 1
1 1 1 1 NA 1
1 1 1 1 1 NA”,header=T)
na.omit(data)
data
> [1] a b c d e f
< 0 line & gt; (or 0-length row.names)

— — — — — — — — — — — — — — — — — — — — —
the author: SThranduil
source: CSDN
,
https://blog.csdn.net/SThranduil/article/details/71710283 copyright statement: this article original articles for bloggers, reproduced please attach link to blog!

No such file or directory

 
No such file or directory can not be found when reading file in R
 
Recently, while reading the file, the following problem occurred
> Passenger = read. CSV (‘ international – the airline – passengers. CSV, sep = ‘, ‘)
Error in File (file, “RT “) : Unable to open link
Warning Message:
In the file (file, “rt”) :
Unable to open file ‘international-Airline-passengers. CSV ‘: No such file or Directory

R can’t find the file.
 
File path, divided into absolute path and relative path, using absolute path is troublesome, usually use relative path, and relative path refers to, relative to the current working path.
 
Using the geTWd () function, you can get the current working path

Solution 1
Set the directory where the international-airline-travel.csv file is placed to the working directory
 
Solution 2
Copy the file international-airline-passengers. CSV to the current working directory

Error in .Call.graphics(C_palette2, .Call(C_palette2, NULL)) : invalid graphics state

I believe my dataframe is okay and my code is okay. In fact, I have eliminated parts of the dataframe and most of the graphing code to make things as basic as possible. But still, I get:

Error in .Call.graphics(C_palette2, .Call(C_palette2, NULL)) : 
  invalid graphics state

What is wrong here?Here is the data:

 date   trt var val
1/8/2008    cc  sw5 0.2684138
1/8/2008    cc  sw15    0.2897586
1/8/2008    cc  sw5 0.2822414
2/8/2008    cc  sw5 0.2494583
2/8/2008    cc  sw5 0.2692917
2/8/2008    cc  sw15    0.2619167
2/8/2008    cc  sw5 0.204375
3/8/2008    cc  sw5 0.2430625
3/8/2008    cc  sw5 0.2654375
3/8/2008    cc  sw5 0.2509583
3/8/2008    cc  sw5 0.2055625
1/8/2008    ccw sw15    0.2212414
1/8/2008    ccw sw5 0.3613448
1/8/2008    ccw sw5 0.2607586
2/8/2008    ccw sw5 0.2087917
2/8/2008    ccw sw15    0.3390417
2/8/2008    ccw sw5 0.2436458
2/8/2008    ccw sw5 0.290875
3/8/2008    ccw sw5 0.20175
3/8/2008    ccw sw15    0.328875
3/8/2008    ccw sw5 0.2328958
3/8/2008    ccw sw5 0.2868958

When I work with this data, I specify dates like this:

df<-df[order(as.Date(df$date,format="%d/%m/%Y")),,drop=FALSE]

and here I want to make a scatterplot:

ggplot(data = df,aes(x = date,y = val)) + geom_point(aes(group = trt))


I ran into this same error and solved it by running:

dev.off()

and then running the plot again. I think the graphics device was messed up earlier somehow by exporting some graphics and it didn’t get reset. This worked for me and it’s simpler than reinstalling ggplot2.

\

R note for Bioinfo: the column for the select call is undefined

R Note for Bioinfo: The column selected to call is undefined
Problems:

input: Table(GPL number)[1: number of rows selected,c(” ID “, “other column attributes”,…)]
Error:
Error in [.data.frame… undefined columns selected
Solution:
Table(GPL number)[1: number of rows selected,c(” ID “, “other column attributes”…)]
note that the other column attributes here are compared with the original database to see if they are consistent
modify the inconsistent column attributes to be consistent with the original database column fields after
error disappears and runs correctly
Specific orders to solve the problem:

Table (GPL) [1:10, c (” ID “, “GB_ACC”, “Gene Title”, “Gene Symbol”, “ENTREZ_GENE_ID”)]

R language learning problem solving error in output $nodeid: $operator is invalid for atomic vectors

Problem: Error in output$nodeID: $operator is invalid for atomic Vectors when viewing variable columns using the “$” operator

output <- data$score
output <- cbind(nodeID=dat$nodeID,score=output)
head(output$nodeID)
 Error in output$nodeID : $ operator is invalid for atomic vectors
# Check the type of output and find out it's matrix.
class(output)
 [1] "matrix"
#"data.frame" can only be used with "$", just use [,] here.
head(output[,1])

 

 

Solutions to the failure of R language loading rjava

library(rJava)

Error: when ‘rJava’ is calculated in loadNamespace(), onLoad failed.

call: inDL (x, as the logical (local), as the logical (now),…).

error: unable to load Shared object ‘f:/Program Files/R/R – 3.1.2/library/rJava/libs/x64/rJava DLL’ :

LoadLibrary failure: %1 is not a valid Win32 application.

Error: failed to load ‘rJava’ package or namespace,

The reason for the above error is that your JAVA version is 32-bit and your R is 64-bit, so download the 64-bit version of the JRE and change the environment variable JAVA_HOME to the position of the 64-bit JRE
The JRE can be downloaded in the website: http://www.java.com/en/download/manual.jsp

R reads JSON data

You can use the Library (JSONLite) package
jsonmessage< -read_json(name.json,simplifyVector = FALSE)
jsonmessage$Month$minute[[1]]$number\
You can use layers of lists to find the information you want
But jsonLite will report an error when reading a Chinese-named JSON file
Error in parse_con(TXT, bigint_as_char) :
circum-error: invalid char in json text.
Now I can change it to Library (RJSONIO)
ts1< -RJSONIO::fromJSON(name.json)
If something is character and you can’t select one of them, just make it a list
ts1$month$minute[[1]]
Name, age, gender, job
Lili 20 female enginner
This string has a noun corresponding to it
To the list
ts1$month$minute[[1]]< -as.list(ts1$month$minute[[1]])
You can turn it into a list and continue to select the items you want

Reproduced in: https://www.cnblogs.com/zhenghuali/p/10455509.html

Interesting undefined columns selected from read.table and read.csv

Enter the following syntax:
read.table(site_file,header=T)-> data
data< -data[which(data[,5]==”ADD”),]
A:
Error in `[.data.frame`(data, , 5) : undefined columns selected
Calls: plot_manhatton -> [ -> [.data.frame -> which -> [ -> [.data.frame
After a few attempts, change the command to:
read.csv(site_file,header=T)-> data
data< -data[which(data[,5]==”ADD”),]
It’s ready to run.
The reason for undefined Columns selected error is that What I imported was a CSV file, but I used read.table when Reading the file. After changing to read.csv, there is no problem.

Reproduced in: https://www.cnblogs.com/chenwenyan/p/5384714.html

Solving the problem of saving object set by save() function in R language

Solve the save() function in R language to save the collection of objects – & GT; The Art of R programming, P195

>ls()
[1] "j"              "joe"            "k"              "o"              "print.employee" "z"             
> z<-rnorm(100000)
> hz<-hist(z)
> save(hz,"hzfile.RData")
Error in save(hz, "hzfile.RData") : The target object 'hzfile.Rdata' does not exist.
> save(hz,"hzfile")
Error in save(hz, "hzfile") : The target 'hzfile' does not exist.
> save(hz,file="hzfile.RData")
> ls()
[1] "hz"             "j"              "joe"            "k"              "o"              "print.employee" "z"             
> rm(hz)
> ls()
[1] "j"              "joe"            "k"              "o"              "print.employee" "z"             
> load("hzfile")
Error in readChar(con, 5L, useBytes = TRUE) : cannot open the connection
In addition: Warning message:
In readChar(con, 5L, useBytes = TRUE) :
  cannot open compressed file 'hzfile', probable reason 'No such file or directory'
> load("hzfile.RData")
> ls()
[1] "hz"             "j"              "joe"            "k"              "o"              "print.employee" "z"             

As shown below:


When you use save(), you use the “file” parameter, and the suffix of the file is “.rdata “. If you use load(), the suffix of the file is “.rdata”