Posts by Rui Barradas • 15,422 points
432 posts
-
4
votes3
answers146
viewsA: Print more variable text
Here are two options: x <- 10 cat("seu numero é", x, "\n") #seu numero é 10 sprintf("seu número é %g", x) #[1] "seu número é 10" Note that cat does not put the string between quotes. See also the…
-
1
votes3
answers137
viewsA: Problem reading readline keyboard input on R
The problem is in the first statement. The function readline shall be executed on a separate line, with or without as.integer. Otherwise, when you switch to the next instruction you are sending a…
-
1
votes1
answer58
viewsA: Problems with sum() function inside summarise using plyr and tidyverse!
These two examples give different values of the sums in each row. The first example is the question, only with ungroup at the end in order to obtain TRUE when the results are compared with the…
-
3
votes1
answer38
viewsA: Why does the "nls" function of R not converge even with the initial curve very close to the points?
The problem lies in the excess of parameters for so few points. If the function to be adjusted is using a translation of the x-axis represented by x - C, one of the parameters can be deleted, C,…
-
0
votes2
answers37
viewsA: Why do R outputs numbers always come out with dot (.)?
Figures formatted as percentages are used for presentation or publication purposes, they are not numbers that can be counted, that is, they are not numbers. It’s not just a matter of scale. With the…
ranswered Rui Barradas 15,422 -
4
votes2
answers98
viewsA: Convert "2012-01-01" to "2012-Jan-01" in R
If you only need the abbreviated month, just take the first 3 letters of that column. This is done with the base R function substr. library(dplyr) library(tidyr) fen <- fen %>% gather(Mes,…
-
2
votes2
answers83
viewsA: Reference one color column based on another
Apparently one of the problems is the possibility of having equal colors for different references. The function fun below solves this problem by creating a color array in which the names are unique…
-
5
votes1
answer28
viewsA: Command line correction R in ggplot2
The problem is the repetition of values in the vector positions. There are only 14 values all of them repeated. So the argument limits Reserve space for 14*2 positions on the axis of y. I believe…
-
2
votes2
answers64
viewsA: str_replace_all - how to find words by the first 3 letters of a string?
It is not necessary to load an external package to make this replacement, the base R has functions sub and gsub that solve the problem. sub("^agr.*\\b", "agr.", filtro_palavras$palavras) # [1]…
-
3
votes2
answers72
viewsA: How to turn a column with a sentence per row into a column where each row is a word of these phrases?
Although there is already an accepted answer, here is a solution with scan. data.frame(palavras = scan(what = character(), text = palavras_cnae_agro[[1]]))…
-
2
votes2
answers333
viewsA: Labels for Plot box in ggplot2
First it is necessary to calculate the positions y labels. To place labels on top of boxes, I will use statistics max and, in geom_text, the argument vjust. df.labs <- aggregate(value ~ ID +…
-
1
votes2
answers172
viewsA: How to remove NA from a dataframe in R?
The following solution can do what the question asks. library(tidyverse) fun <- function(x) {sub("(^.*\\d,\\d\\d)(.*$)", "\\1", sub("\\.", ",", x))} df %>% mutate(across(everything(),…
-
3
votes1
answer53
viewsA: Is it possible to check an error of a function with an if()?
The following code reads the keyword array and creates two lists, a list of results and a list of errors. library(readr) library(gtrendsR) keywords <-…
-
1
votes3
answers95
viewsA: Remove data frame row with string divided into multiple columns
The following function reads the file with readLines as if it were a text file, does a preprocessing, removing the first and last lines and creates a connection with the resulting text in the form…
ranswered Rui Barradas 15,422 -
2
votes2
answers84
viewsA: Warning message: Those produced when calculating confidence intervals
I do not know if the following is what the question asks. intconf <- function(x, nivel = 0.95, normal = FALSE){ qq <- c((1 - nivel)/2, 1 - (1 - nivel)/2) n <- length(x) xbar <- mean(x)…
ranswered Rui Barradas 15,422 -
2
votes1
answer31
viewsA: How to group identifiers that are related across rows and columns in R
A solution can be found with the package igraph, for graph problems. It seems natural to treat the question problem as a problem of finding the connected components of a graph. 1. Load the package…
-
5
votes1
answer110
viewsA: (R Language) How to ask a user for data frame input?
To get an R object from a string with its name, use the function get. Note: I renamed the string to dataframe, without the point, since data.frame is already the name of a base R function. dataframe…
ranswered Rui Barradas 15,422 -
2
votes1
answer111
viewsA: Please I need to calculate the SD by several variable rows (columns),
With the question data, the following code calculates the standard deviations of Line 1; Lines 1 and 2; Lines 1, 2 and 3; Lines 1, 2, 3 and 4. But the results do not match the results in the…
ranswered Rui Barradas 15,422 -
2
votes1
answer162
viewsA: Repeat the value of a line in the rows immediately below a dataframe in R
The following R base code solves the problem. f <- cumsum(!is.na(dados$COD_PART)) dados$COD_PART <- ave(dados$COD_PART, f, FUN = function(x){x[-1] <- x[1]; x})…
-
4
votes1
answer88
viewsA: how to find the average in a table of classes
One way to calculate weighted averages is with weighted.mean. with(cartorio, weighted.mean(p.m, porcentagem)) #[1] 6.9 Gives the same result as the solution in commenting user’s Carlos Eduardo…
ranswered Rui Barradas 15,422 -
1
votes2
answers59
viewsA: Chi-Square calculation for proportions
I do not believe that the data as it is in the question is sufficient to carry out a chi-square independence test. In order to do this, data are needed to calculate counts (contingency table) and…
ranswered Rui Barradas 15,422 -
2
votes3
answers702
viewsA: Add elements into a vector using loops and inputs
A way without cycles for is to read from stdin() with the function scan. Just pass the total numbers read in the argument n. a <- scan(stdin(), what = character(), n = 5, quiet = TRUE) a <-…
ranswered Rui Barradas 15,422 -
3
votes2
answers172
viewsA: Error when trying to run nls
Version of R and packages minpack.lm and ggplot2. R.version.string #[1] "R version 4.0.1 (2020-06-06)" packageVersion('minpack.lm') #[1] ‘1.2.1’ packageVersion('ggplot2') #[1] ‘3.3.0’ Version of…
ranswered Rui Barradas 15,422 -
3
votes1
answer338
viewsA: Change plotting order of an area chart in R
Here are two solutions to the problem. Both order the countries of the largest somaacumulada for the smallest but do it in different ways and the final orders are different. In both solutions…
-
0
votes2
answers58
viewsA: How can I filter the first occurrences of a certain variable in my R data frame?
This solution dplyr obtains the dates of the first cases confirmed by "uf". First dates become class "Date" and confirmed in class "numeric". library(dplyr) df <- df %>% mutate(date =…
ranswered Rui Barradas 15,422 -
0
votes2
answers43
viewsA: I want to overlay two graphs
The problem As Marcus Nunes says in his reply, the problem comes from calculating the mass probability function of the binomial distribution. Factorials grow very fast and an overflow condition…
ranswered Rui Barradas 15,422 -
2
votes2
answers294
viewsA: How to plot distinct regression models using the ggplot2 + ggpmisc or gridExtra packages?
I believe the question is as follows:. To have two Fits, just call geom_smooth twice, once for each formula. And then add the two equations, adjusting the coordinates of the second equation so that…
-
0
votes1
answer38
viewsA: I would like to leave the data.frame without repeated elements used this code:
It is not necessary to repeat the calls to sample, once is enough. As for the final result, it is not clear which format is intended, this code data.frame only with one column and no repetitions. f…
-
4
votes1
answer47
viewsA: Curve does not fit on the Lines
With the question data here are two ways to plot the graph with the predicted curve. R base The entangled line problem is solved by sorting the x-axis variable and then using this ordered variable…
ranswered Rui Barradas 15,422 -
2
votes1
answer68
viewsA: Error in nls: step factor 4.65661e-10 reduced below 'minFactor' of 9.31323e-10
With the problem data, the following modifications Reduce maxiter to 100; include tol = eps, with eps = .Machine$double.eps^0.5; Use the algorithm 'port' instead of 'plinear'. achieved convergence…
ranswered Rui Barradas 15,422 -
4
votes1
answer44
viewsA: How to use which.max in a long-format dataframe?
To do what the question asks First filter by ISO code, you get a smaller base which is better for what comes next. Group by ISO code, calculations will be made country by country. Calculate the new…
-
5
votes1
answer32
viewsA: How to use ggTS with ggplot?
To install just clone the repository or download the . zip file. ggTS is not a package is a function R, which depends on the following CRAN packages: pacotes <- c('gsw', 'dplyr', 'purrr',…
-
2
votes1
answer32
viewsA: How to order one dataframe by order of another?
The function order is the reverse itself. So, if you order the order of DF2 by the reverse of the order of DF1, we have DF2 by the required order. Complicated? the code is even very simple. i1 <-…
-
3
votes3
answers1427
viewsA: Moving average in R
Beyond the answers that have already been given, (1) and (2), my answer is simpler (but it does not explain the reason for the error of the question) because it uses The function rollmeanr which by…
ranswered Rui Barradas 15,422 -
7
votes1
answer72
viewsA: Problem with key in ggplot, using double axis y
This type of problem is usually easier to solve if the data is in the long format. This allows having a single variable to be used in aes() for colour, linetype, shape or any other. The code can get…
-
1
votes2
answers51
viewsA: Apply function by groups or factors to R
This is a basic R solution. Adopts the strategy split/apply/combine hadley Wickham. The function var_rel is rewritten to have a single argument. var_rel <- function(x){ 100*(x[2] - x[1])/x[1] }…
ranswered Rui Barradas 15,422 -
0
votes1
answer129
viewsA: Error in notamf(mf) : p object not found
The following works and is simpler. Here are some of the errors in the original code. Never define functions within the cycle for. The functions computed values from what the user entered but these…
ranswered Rui Barradas 15,422 -
4
votes1
answer71
viewsA: Seasonality chart with ggplot2
First of all, the data must be prepared with casosNovos == 0 and with aggregation functions to calculate averages and medians. The function to be used to read the file is read.csv2 once the decimals…
-
3
votes1
answer116
viewsA: How to change the graph structure in ggplot2?
The following solution uses the function sec_axis with a transformation identical to that of geom_point which reverses the values of Spp = "Ca". library(ggplot2) ggplot() + geom_point(data =…
-
3
votes3
answers56
viewsA: How to Capture a File Extension in R
This function uses basename to get filenames without directories. Then check whether filenames have a dot or not. Finally, extract only what is between the last point "." and the end of filenames.…
ranswered Rui Barradas 15,422 -
2
votes1
answer542
viewsA: To change the order of the legend without changing the colors of the chart in ggplot2
To change the order of the legend, simply change the order of the variable levels fill in scale_fill_brewer with the argument breaks. Below is an example with the base mtcars. The chart on the left…
-
4
votes2
answers1104
viewsA: How to eliminate variables that have some NA values in R
A way in R base is as follows. To each base column apply (sapply) the function anyNA. dados[, !sapply(dados, anyNA)] # Cidade Estado populacao #1 Salvador BA 21212 #2 Salvador BA 21212 #3 Salvador…
ranswered Rui Barradas 15,422 -
2
votes2
answers41
viewsA: Form a dataframe from 3
R base A solution only in R base can be with Reduce/merge. First, read the files. These two instructions assume that the only files that are in the current directory with the extension "csv" are the…
ranswered Rui Barradas 15,422 -
3
votes2
answers242
viewsA: Format latitude and numerical longitude coordinates in R
I believe that this role solves the problem. In the question it is said that there are always two digits before the decimal point, even if there have been errors in the base. Then the function…
-
3
votes1
answer34
viewsA: How to create a range of an xls file, which is fed weekly?
You can use the function cell_limits package cellranger to define an open rectangle. The format is ul - vector identifying the upper left corner (upper lEft) of the rectangle cell. lr - vector…
ranswered Rui Barradas 15,422 -
2
votes1
answer62
viewsA: Dotplot in R include vertical line and dots of different colors
First load the used packets and create the data in a reproducible way, with set.seed. library(dplyr) library(lattice) set.seed(1234) n <- 1000 df <- tibble( xx1 = runif(n, min = 3, max = 10),…
-
2
votes3
answers339
viewsA: How to convert the number of hours into date format in r
The following function converts the dates with hours into numbers with decimals into dates with hours in the format HH:MM:SS. dec2hour <- function(x){ d <- sub("(^.*) .*$", "\\1", x) h <-…
-
0
votes2
answers1362
viewsA: Warning message: Those introduced by coercion
Note: This is not an answer, it is a broad comment, trying to explain a possible way to solve the problem. First a dataset with a class column "character" to transform into "numeric". t2019_12 <-…
ranswered Rui Barradas 15,422 -
3
votes1
answer61
viewsA: Color the axis source of a dendrogram in ggplot2 according to a categorical variable
The function element_text has an argument color which, although not officially vectorized, works with vectors. Like Species is a column of the base iris class "factor", can be used as an index of a…
-
2
votes1
answer97
viewsA: mutate - case_when - Rstudio
The question code has two problems: As quotation marks "smart" or curves; There are too many spaces in the target strings. Fixed these problems works well. library(dplyr) library(stringr) BWn <-…
ranswered Rui Barradas 15,422