Posts by Marcus Nunes • 17,915 points
372 posts
-
6
votes2
answers59
viewsA: How does R calculate the following code?
This is because R follows the conventions of mathematics. First it is made the potentiation and its inverse operation, then multiplication and its inverse and, finally, addition and its inverse.…
ranswered Marcus Nunes 17,915 -
3
votes2
answers333
viewsA: Labels for Plot box in ggplot2
Most graphics made with the ggplot2 follows a scheme of the type ggplot(dados, aes(x = VariavelX, y = VariavelY)) In the case of your problem, the variable to be placed on the X axis is ID, while…
-
3
votes1
answer46
viewsA: Composition Graph using ggplot2
The first step is to put the data in the long format. Fortunately, the package reshape2 makes this job a lot easier: x<-c(0, 1.52, 8.12, 0, 0.29, 0, 3, 4, 1.2, 1.1) y<-c(4.8, 3.03, 6.82, 9.76,…
-
2
votes3
answers95
viewsA: Remove data frame row with string divided into multiple columns
If the data frame is called dados and the goal is to always remove your last line, do dados.limpos <- head(dados, -1) to create the data frame dados.limpos, identical to the object dados, but…
ranswered Marcus Nunes 17,915 -
1
votes1
answer78
viewsA: How to replace lost data frame values with the average of each column in R?
Can you do with the dplyr yes. Just use the function mutate_all, indicating where values should be changed (is.na) and how they should be filled (mean with the argument na.rm = TRUE):…
-
3
votes2
answers81
viewsA: How to join more than two dataframes in R?
The functions *_join, package dplyr, are only set for operations with two objects at a time. Fortunately it is possible to apply them to more than one object simultaneously using the function reduce…
-
1
votes2
answers462
viewsA: How to sort data on Y-axis using ggplot2 in R
Another way to solve this problem is by using a regular expression to extract only the numbers present in DNAsat. The advantage of this method is to avoid over-typing, which can lead to errors.…
-
3
votes2
answers43
viewsA: I want to overlay two graphs
The R will always plot the points in the order they appear in the vector. See the example below, in which I plot the function x 2: x <- c(1, 5, 3, 6, 2, 4) plot(x, x^2, type = "l") See how the…
ranswered Marcus Nunes 17,915 -
2
votes1
answer48
viewsA: How to make a double filter on a long-format dataframe
I’d do it this way: Would eliminate all lines with NA in total_tests Convert date for date, as it is possible to establish an order relation in that column Sort the data frame by country and for…
-
3
votes1
answer1724
viewsA: How to change the color scale on a chart in R
There are two main ways to define a color scale using ggplot2. One is manual and the other is using a preset color palette. Setting colors manually Each color should be defined manually. This…
-
4
votes1
answer45
viewsA: Loop with R arrays
It is not necessary to create a loop for this. The function expand.grid does exactly what is requested: expand.grid(1:3, 1:3) Var1 Var2 1 1 1 2 2 1 3 3 1 4 1 2 5 2 2 6 3 2 7 1 3 8 2 3 9 3 3 Just…
-
5
votes1
answer62
viewsA: How to plot negative coordinates (South Pole) using ggmap, ggplot?
The problem lies in the coordinates of ylim. Reverse their order and it will all work out. Look at the Drake Strait down there, as it should be. library(rnaturalearth) library(ggplot2) world <-…
-
5
votes1
answer438
viewsA: How to increase Y-axis scale marks in graphics in ggplot2?
Use the argument breaks of function scale_y_continous: dados %>% filter(casosAcumulados > 9) %>% group_by(estado) %>% mutate(diasposdez = 1:n()) %>% ggplot(aes(diasposdez,…
-
4
votes1
answer114
viewsA: How to filter data by a text pattern in R
Use the function grep. It allows you to search string snippets. By combining it with the function filter package dplyr, it is possible to keep only rows of the column region_slug who possess…
-
2
votes3
answers56
viewsA: How to Capture a File Extension in R
With R base, you can use the function strsplit: arquivo <- "dados/Inscritos.xls" extensao <- unlist(strsplit(arquivo, split = "\\."))[2] As the result of strsplit is a list, it is necessary to…
ranswered Marcus Nunes 17,915 -
3
votes1
answer24
viewsA: How to find time-series loopholes?
It seems to me that the Join that solves your problem is the left_join. See below: library(dplyr) library(lubridate) # criacao dos dias de referencia dias <- seq.Date(from = dmy("01-01-2001"), to…
-
4
votes1
answer129
viewsA: How to put captions on a line chart in ggplo2
The best way to plot data the way you want is to put it in the long format. Read this question right here at Stackoverflow to find out what this is. In short, you need to create a column in your…
-
3
votes2
answers41
viewsA: Form a dataframe from 3
The function left_join package dplyr does exactly what is requested. However, it only works with two data frames at a time, so it needs to be applied in two opportunities. Assuming that data frames…
ranswered Marcus Nunes 17,915 -
3
votes1
answer54
viewsA: How to make a GLM with different sample n?
It is not possible. A generalized linear model is the relationship between the response vector Y and the outline matrix X represented in the formula below: By definition, X cannot have missing data.…
-
1
votes1
answer61
viewsQ: Color the axis source of a dendrogram in ggplot2 according to a categorical variable
Suppose I need to build a dendrogram on R: library(tidyverse) library(ggdendro) ggdendrogram(hclust(dist(iris[, -5]))) + theme(text = element_text(size = 8)) My chart is created, but I want to…
-
2
votes2
answers123
viewsA: How to increase the number of iterations in R?
Just use the function nls.control within the function nls to obtain the desired result: library(data.table) library(tidyverse) setDT(data) data[,Day:= as.Date(Day, "%d/%b")] data[,Int :=…
ranswered Marcus Nunes 17,915 -
3
votes1
answer112
viewsA: Calling Loops dataframes in R
The function get does exactly what is necessary: she understands that you are looking for an object present in Workspace called nome_i, with this index i varying. See an example of how to use it…
-
1
votes1
answer33
viewsA: Obtaining vehicle forecast (Interpretation of output)
The adjusted model is an ARIMA(3,0,5). In other words, it is a ARMA model(3,5). It is known that the prediction m forward steps of a ARMA model(p, q) is given by As the ARMA(p, q) model is…
-
1
votes1
answer655
viewsQ: Reorder the columns of a bar chart and its caption
When processing and plotting data in R using tidyverse, it by default leaves the chart bar order in the original column factor order: library(tidyverse) mpg$trans <- as.factor(mpg$trans) mpg…
-
4
votes1
answer45
viewsA: Define limitation on generated random numbers
For each individual weight, simply select a random number from any distribution supported by the non-negative numbers. Then divide each weight by the sum of all weights: set.seed(1234)…
-
4
votes1
answer403
viewsA: Convert date to month in full
Just use the function month package lubridate: library(lubridate) data = c("01", "03", "03", "04", "05", "10", "12") month(as.numeric(data), label = TRUE, locale = "pt_BR") ## [1] Jan Mar Mar Abr…
ranswered Marcus Nunes 17,915 -
5
votes1
answer71
viewsQ: How to create a heatmap for a calendar?
One of the graphs I find most interesting is called, in English, heatmap Calendar. Perhaps its most common application is in github, that displays our collaborations in the last year with the chart…
-
2
votes1
answer138
viewsA: How to avoid displacement of a forecast using ARIMA?
This displacement cannot be avoided. It is an inherent feature of the ARIMA(p,d,q) models. In particular, the AR(p) models, which are part of the ARIMA(p,d,q). ARIMA is the English acronym for…
-
5
votes1
answer144
viewsA: Italics in dashboard headings in facet_wrap()
No help from function ggplot2::theme, it is possible to find the argument strip.text. His description is as follows:: facet labels (element_text(); inherits from text). So just change the option to…
-
4
votes3
answers100
viewsA: How to generate graphics from a file using a loop in R?
By definition, the ggplot2 do not plot the graphics if they are being generated within a loop. In order for the graphics to appear, it is necessary to plot them explicitly through the function…
-
4
votes1
answer250
viewsA: Error: numeric send argument has no length one
Rather, two definitions: A vector is a collection of n elements of the same type. The function lm means linear model (linear model). Simplifying much, in its simplest version, what it does is to…
-
3
votes4
answers101
viewsQ: Dealing with dates of heterogeneous formats in R
I have 236 files in . csv that have all the same columns. My goal is to join them all into one data frame only. However, each of them has 4 columns with date and time values. The problem is in the…
-
1
votes1
answer193
viewsA: Change y-axis scale to show decimal differences on a bar graph
Determine the limits of the y-axis with the argument ylim within the function barplot. In this case, I defined that the axis should go from the minimum of fvcmt up to the maximum of fvcmt,…
-
4
votes1
answer417
viewsA: How to use auto.Rima to predict 24 periods or more in R?
I will try to answer as best I can the questions posed in the reward of this question. I understand that Data Science is not perfect, and when it comes to data in our day-to-day life it becomes…
ranswered Marcus Nunes 17,915 -
2
votes2
answers70
viewsA: A: How to create/save a vector using for and Paste?
You can do exactly what is asked using the function assign: for(i in 1:3) { nome <- paste("vetor_", i, sep = "") assign(nome, rnorm(10)) }
ranswered Marcus Nunes 17,915 -
5
votes2
answers826
viewsA: How to define the number of clusters in the Kmeans algorithm in R?
Finding the ideal number of clusters is not a trivial task. In general, unsupervised learning tasks are complicated to solve precisely because we don’t know the answer to the problem. Logical, when…
-
2
votes1
answer405
viewsA: How to rename caption and caption values in fviz_cluster()
The function factoextra::fviz_cluster it’s just one wrapper for the function ggpubr::ggscatter. Therefore, simply do not map the color and shape of the dots explicitly so that the unwanted caption…
-
4
votes1
answer101
viewsA: Bootstrap in linear regression model - Calculating the importance of variables
Note that in function help boot, the argument statistic has the following description (emphasis added): A Function which when Applied to data Returns a vector containing the statistic(s) of…
-
7
votes3
answers309
viewsA: How to replace variables with NEGATIVE values with their ABSOLUTE VALUE within a date.frame in R?
Using the function abs: library(dplyr) Dados %>% mutate_if(is.numeric, abs) ## Linha A B C D E ## 1 L1 4 3 1 2 4 ## 2 L2 1 2 1 5 1 ## 3 L3 1 1 2 3 4 ## 4 L4 2 4 5 7 9…
-
2
votes1
answer73
viewsA: Create column names with spaces
One way to solve this is to write the variable name between bass accents: MUNICIPIO <- c('BELO HORIZONTE', 'BRASILIA', 'JUIZ DE FORA', 'MANAUS', 'MONTES CLAROS', 'RECIFE', 'RIO DE JANEIRO',…
ranswered Marcus Nunes 17,915 -
5
votes1
answer33
viewsA: Some error bars do not appear on "sciplot" graph
The Warning gives a good idea of what is happening (my griffins): Warning messages: 1: In Arrows(Leg.vals$xvals[CI.seln], CI.Plot[, 1], Leg.vals$xvals[CI.seln], : zero-length Arrow is of…
ranswered Marcus Nunes 17,915 -
1
votes1
answer18
viewsA: Compile information on the same line
It is possible to obtain the desired result with the code below, which groups the results by Name calculates the maximum value of columns Week and Weekend: df %>% group_by(Name) %>%…
-
2
votes1
answer101
viewsA: How to save a rpart.Plot chart?
The R has a series of commands to save figures in bmp, jpeg, png, tiff and pdf formats. Below is an example of how to create a graph in png format using one of these commands. png("arquivo.png")…
-
5
votes1
answer57
viewsA: I cannot use some fonts in Rmarkdown
The font definition for the specific use of Calligra is not done in the usual way. As it is defined from a package, it is necessary to explicitly determine an Environment, so that this source is…
-
1
votes2
answers509
viewsA: How to create a sequence of dummy variables with loop in r
The package onehot does this automatically: library(onehot) empresas <- data.frame( empresas = sample(c("GLO", "AZU"), 10, replace = TRUE) ) empresas ## empresas ## 1 AZU ## 2 AZU ## 3 GLO ## 4…
-
2
votes1
answer66
viewsA: Standard error bars do not appear in R graph
No help from function sciplot::lineplot.CI, we see the following argument: err.width = if(length(levels(as.factor(x.factor))) > 10) 0 else 0.1 That is, if the number of levels on the axis x is…
ranswered Marcus Nunes 17,915 -
2
votes2
answers634
viewsA: How to include a value in the last line of a data.frame in R
Create the data frame z with the desired observations, making sure that the names of their columns match the names of the columns of df: x <- c(1:15) y <- c(1:15) df <- data.frame(x,y)…
ranswered Marcus Nunes 17,915 -
4
votes1
answer155
viewsA: Bar graph ggplot in R
Just put the argument show.legend = FALSE within the function geom_label: ggplot(data=CS, aes(x=Situação, y=Quantidade, fill=Setores)) + geom_bar(aes(fill = Setores), stat="identity",…
-
1
votes1
answer93
viewsA: How to compare information from one table to another table
Use the function inner_join package dplyr: df1 <- data.frame(nome = c("Ana", "Bernardo", "Carlos"), telefone = c("123", "456", "789"), altura = c(1.70, 1.75, 1.80)) ## nome telefone altura ## 1…
ranswered Marcus Nunes 17,915 -
2
votes1
answer45
viewsA: Plot the PCA result in three dimensions
Use the function pca3d of the package of the same name: library(pca3d) irisPCA <- prcomp(iris[, -5], center = TRUE, scale. = TRUE) pca3d(irisPCA, group = iris$Species)…