Posts by Rui Barradas • 15,422 points
432 posts
-
1
votes1
answer225
viewsA: How to make a cross from a matrix in R
The data are not in the correct order and are incomplete, in the question there are only 9 lines when 12 are needed, as many as the points of the figure. I include the corrected data at the end.…
-
1
votes1
answer52
viewsA: Error in program something related to the lines
To correct the error of the question data, I used the function rep to create the vector enem. The variable curso has been defined as being categorical or "factor" to keep the chart bar order down.…
-
3
votes1
answer30
viewsA: R splinefun with NA
Can use tryCatch to catch code execution errors and then act on the result. I also created an auxiliary function, is.error, to test the output of splinefun. is.error <- function(x) inherits(x,…
ranswered Rui Barradas 15,422 -
4
votes2
answers2399
viewsA: How to replace semicolon by dot in a dataframe in R Studio
One way to replace decimal commas with dots is with as.numeric/sub. Since the question dataframe is not very clear, I’m not sure if the first column is even a column or if it is the row.names, I…
-
4
votes2
answers78
viewsA: How to assemble a matrix in R being the values the subtraction of values of a vector
The function outer was made to answer the question problem. By default it assumes the function "*" to calculate the external product (Wikipedia Português, in English), but any other function may be…
-
5
votes2
answers91
viewsA: Extract information from a string
Can extract prices with a combination of gregexpr and of regmatches. In what follows I have two solutions to be used according to the structure of prices in Brazil (I am Portuguese). If a price may…
-
2
votes1
answer47
viewsA: Generate BEKK latex PDF and diagnoseBEKK of the 'mgarchBEKK' package
You can create a latex table for each array of estimates with the following function. I will not use the package stargazer but yes the package xtable, that is simpler and that for this problem…
ranswered Rui Barradas 15,422 -
0
votes3
answers119
viewsA: Problems with installing the SSOAP package on R
To install R packages that are not available for the R version you are running, go see if they are in cran files. Then there are several ways to install these packages. I will assume that the user…
ranswered Rui Barradas 15,422 -
1
votes2
answers942
viewsA: Loop R to find the highest value in a column
Despite the answer by Willian Vieira be, in a certain sense, correct, it should be noted that maybe it is not the solution for all cases. The function which.min (and the corresponding which.max)…
ranswered Rui Barradas 15,422 -
2
votes1
answer71
viewsA: How to not generate BEKK11 directly on the screen
To have all the values you can put the ones you want in the output list of the function. BEKK11 <- list(Original = list(data = rt, estimates = est, HessianMtx = Hessian, Sigma.t = Sigma.t,…
ranswered Rui Barradas 15,422 -
5
votes2
answers190
viewsA: How to insert lines in the graphs to denote post-hoc differences between compared groups?
As it is in the comment of Carlos Eduardo Lagosta, the package ggpubr has a function, stat_compare_means which can insert the comparison lines. In the case of the example below, these comparisons…
-
1
votes1
answer612
viewsA: How to break a line in the title of the graph into 2 lines using ggplot?
You can break the title with title and subtitle. And to have text in italics, you can do bquote and plotmath. library(ggplot2) ggplot(dados, aes(grupo, a)) + stat_boxplot(geom = "errorbar", size =…
-
6
votes2
answers84
viewsA: Plot is different from function values
The problem is the use of && instead of &. Of documentation, help('&&') (my emphasis.): & and && indicate Logical AND | and || indicate Logical OR. The Shorter form…
ranswered Rui Barradas 15,422 -
2
votes1
answer52
viewsA: Graph in R - ggbarplot - Axis X with Supervisor / Date
The following code does what the question asks. The changes were: With the package dplyr: extract the first name of SUPERVISOR. Turn date/time into date only. On the chart: lab.size = 3 for…
ranswered Rui Barradas 15,422 -
3
votes2
answers200
viewsA: How to modify only the line of the legend that identifies the group?
A solution can be found in Stack Overflow in English. I’ll still use the chart from reply user’s João Pedro Bazzo Vieira. Is used guides to give another value to aesthetic size with override.aes,…
-
3
votes1
answer39
viewsA: Use lapply inside loop for?
You can do it many ways, here are three of them. In both the whole work is done by apply(dados, 2, sample, n, TRUE) The difference is in how to call this instruction. 1st Combine replicate with…
ranswered Rui Barradas 15,422 -
2
votes1
answer170
viewsA: Aggregating values in R
The question code has the variable to aggregate, VALOR_DIA, where the aggregation variable should be. And the aggregation variable, which cannot be the whole basis, is missing a class object…
ranswered Rui Barradas 15,422 -
1
votes2
answers72
viewsA: How to Find a value between columns
One can use %in% to create a logical index. As the new values are equal to the vector Valor, I’ll initialize the vector Encontrado with the vector Valor. Then I replace only the ones where the…
ranswered Rui Barradas 15,422 -
5
votes1
answer89
viewsA: Removing lines from a problematic database in R
I believe the following code R, with some awk preparatory, does what the question asks. First I’ll redirect the command output iconv question for a new file, the file 2019_Viagem_UTF8.csv. iconv -f…
-
4
votes1
answer60
viewsA: A - Create control column from two other columns
I’ll do it in two lines of code. Just see that the logical values FALSE/TRUE are coded internally by R as integers 0/1, respectively. Then, it is first seen that the values of the two columns are…
ranswered Rui Barradas 15,422 -
0
votes1
answer297
viewsA: Filter with 2 variable conditions
The data of the question are in a language (?) that is not the R nor another that I know, but I believe that with some effort one understands what is intended. The values of col1 must be equal in…
-
1
votes1
answer208
viewsA: How to Find Higher or Lower Values between Columns within a Data Frame in R
The following code calculates for each value of V_de_Busca the first value of V_Encontrado greater than this value of V_de_Busca. x$Maior <- sapply(seq.int(nrow(x)), function(i){ if(x[i,…
-
1
votes1
answer45
viewsA: Average the sum of two columns and division of two columns
Here is a way to do what the question asks, only with R base. First we make a copy of the base columns that will be processed. And we turn their vectors into numerical vectors, keeping in mind that…
ranswered Rui Barradas 15,422 -
1
votes2
answers52
viewsA: Create new columns of data in a data frame
You can do what the question asks in the following fully vectorized way. This way uses the package stringi. library(stringi) censo2$Minimo <- NA censo2$Maximo <- NA grupos <-…
ranswered Rui Barradas 15,422 -
1
votes2
answers140
viewsA: Delete lines with a specific string
The following does what the question asks. i_col <- grep("área.dos.estabelecimentos", names(censo), ignore.case = TRUE) i_linha <- grepl("-", censo[[i_col]]) censo2 <- censo[!i_linha, ]…
ranswered Rui Barradas 15,422 -
3
votes2
answers694
viewsA: Double bar graph
The best way to do what the question asks is to use the package ggplot2. I’ll still use the package reshape2 to reformat data from wide format to long format. library(ggplot2) longo <-…
-
4
votes2
answers104
viewsA: R straight location of x in y
One way is to adjust a linear model, since the graph of (x, dado) is almost a straight. This value is common to the two charts below. tercil <- quantile(x, probs = 1/3) Now, I see the chart only…
ranswered Rui Barradas 15,422 -
3
votes1
answer847
viewsA: loop For, R - Include function in For and save result to object
You can do what you want with two applications of lapply, the best way to do it. With the cycle for and assign, gets several dataframes in .GlobalEnv, with lapply keeps a list that keeps them all in…
-
0
votes1
answer106
viewsA: How to generate charts of daily, monthly and annual averages in time series data in R?
These two solutions, one for daily averages and the other for monthly averages, are very similar and use the package dplyr. library(readxl) library(lubridate) library(ggplot2) temperatura <-…
-
2
votes1
answer33
viewsA: Problems to transform variable data timeSeries in R
Try the following function. Keep the line names. ls() #[1] "Log_retorno" splitByColumn <- function(DF, envir = .GlobalEnv){ r <- lapply(names(DF), function(x) { assign(x, DF[, x, drop =…
ranswered Rui Barradas 15,422 -
3
votes1
answer64
viewsA: Loop command to group values from a database into a new list
I believe the following does what the question asks. Explanation: The function first determines which columns of interest with grep. Then use lapply to go to each vector column and get only the…
-
2
votes1
answer95
viewsA: Fitdist problems in beta distribution
The error is in trying to adjust a beta to data that is not in the range [0, 1], the beta distribution support. The data must first be transformed. ScaleData <- function(x, na.rm = TRUE) { (x -…
ranswered Rui Barradas 15,422 -
3
votes3
answers2072
viewsA: How to join two data.frames of different sizes per column in R?
Only with R base, function merge with the argument all = TRUE does what the question asks. set.seed(1234) a <- data.frame(linha = 1:4, x = replicate(n = 2, expr = sample(0:5, 4, TRUE)) ) b <-…
ranswered Rui Barradas 15,422 -
3
votes3
answers1493
viewsA: How to number lines of a data.frame in R?
With the package dplyr, a simple way is as follows. dados <- dados %>% mutate(numeracao = row_number()) dados # letra N1 N2 N3 N4 numeracao #1 A 2 3 4 4 1 #2 A 1 2 3 4 2 #3 A 2 2 1 3 3 #4 B 0…
ranswered Rui Barradas 15,422 -
4
votes1
answer40
viewsA: How to reference columns in a R function?
One way to do what the question asks is with deparse/substitute, which transforms the variables passed into function arguments without quotation marks into strings (the variable names), followed by…
-
4
votes1
answer64
viewsA: How to extract separate data from an out-of-format string
I believe this regular expression solves the problem. The data are these: a <- c("João Fernando Freitas 123546514 sdfasfasfa", "WDFG V/AA 8952 123546514 sdfasfasfa") And the regular expression. b…
-
2
votes2
answers450
viewsA: Bar graph with ggplot2
First you have to reformat the data, wide to long format. long <- reshape2::melt(dados, id.vars = 'Ordem') head(long) # Ordem variable value #1 Hymenoptera Bosque 192 #2 Diptera Bosque 135 #3…
-
1
votes1
answer32
viewsA: Do a function that returns the average of the notes that person participates in R
The following function does what the question asks. No cycles are required for, is all vectored. The main job is to split the column "Personagens" by commas and then use function lengths to obtain…
ranswered Rui Barradas 15,422 -
1
votes1
answer34
viewsA: R <- Vector - Error in c(vector_item, v$Qty) : Object 'vector_item' not found
To solve the immediate problem, correct the error, as I say in the question comment can create the vector vector_item <- NULL or, alternatively, vector_item <- c() before the cycle for. But it…
-
4
votes2
answers169
viewsA: Remove all after the second occurrence of a comma
Here it comes. sub("(^[^,]*,[^,]*),.*$", "\\1", exemplo) #[1] "Rua Pajé, 30" "Av. Brasil,55" Explanation. [^,] corresponds to any character except the comma. The circumflex as the first character…
-
2
votes4
answers1763
viewsA: How to know which is the largest variable of a vector in R?
First the vector must have names, only then can they be extracted. You can do it like this: vetor = c(x, y, z) vetor <- setNames(vetor, c('x', 'y', 'z')) vetor #x y z #2 3 5 The names are on the…
-
3
votes3
answers193
viewsA: Conditional formatting on line R
Using the function na.locfpackage zoo, makes it easier to do what you want. df$N_VOLTAS <- apply(df, 1, function(x) { y <- zoo::na.locf(x) y[length(y)] }) Dice. df <- data.frame(VAR1 =…
-
3
votes1
answer31
viewsA: transfer empty spaces from the database to the last column in the R
This solution assumes that the data should be numerical but with the value '' have become characters or factors. So first they turn into numbers, which makes the '' stay NA. dados3[] <-…
ranswered Rui Barradas 15,422 -
3
votes2
answers80
viewsA: Form Equation in R-Multiple Regression
Simply adjust a linear model with an independent term, the intersection with the yy axis. Y <- c(81700, 73300, 89500, 79800, 69900) X1 = c(38,46,39,43,32) X2 = c(4,0,5,2,4) modelo <- lm(Y ~ X1…
ranswered Rui Barradas 15,422 -
2
votes1
answer104
viewsA: Turn time series into base 100 on R
There is no R function to do this, it is therefore necessary to define a. base100 <- function(x, na.rm = FALSE){ if(na.rm) x <- na.omit(x) 100*x/x[1] } set.seed(1234) y <-…
ranswered Rui Barradas 15,422 -
4
votes1
answer553
viewsA: Error trying to draw graph: "length of the larger object is not multiple of the length of the smaller object"
The main problem is that the function is not vectorized. The second problem is that integrate returns a list, but stat_function needs a number vector. So, first you correct the function. my_gamma…
-
1
votes2
answers2242
viewsA: Show Data Labels in Column Chart in R (ggplot2)
This solution uses the package dplyr to pre-process the data. library(ggplot2) library(dplyr) Vendas %>% group_by(ID_LOC) %>% summarise(n = n()) %>% ggplot(aes(ID_LOC, n, label = n)) +…
-
6
votes2
answers373
viewsA: How to calculate the average excluding zeroes in R?
Just index the vector to exclude zeros. x <- c(12,20,15,0,7,0) mean(x[x != 0]) #[1] 13.5 If the vector has values NA, use the argument na.rm = TRUE or the function which. y <- x y[3] <- NA…
-
3
votes1
answer1129
viewsA: Calculate derivative and integrals in R
See if this is what you want. The trick is to use body to give value to the body of the function, as it is on the help page help('body'). calcula <- function(e, k, a, b){ D.x <- D(e, "x") f…
ranswered Rui Barradas 15,422 -
3
votes3
answers1276
viewsA: A: Creating a new variable using if and Else
On the basis of bbiasi response with x moved to iso_pais as it comes in the question, and using only R base one can make of these two following modes. The first is in fact the first ifelse from…