Posts by Jorge Mendes • 1,623 points
44 posts
-
1
votes1
answer25
viewsA: How to verify if an ID has different information in a period of time?
I tried with 2 aggregations. The first with cnpj and Uf to create the flag column for pairs without affiliate. And in the second I summed up the values of flags by Uf and year, which are the…
-
2
votes3
answers44
viewsA: How to index subgroups in R
Use the little trick of cumsum, which then adds 1 when a b appears in the group. dados <- data.frame(processos = c("123","123","123","abc","abc","xyz","xyz","xyz","xyz"), situacao =…
ranswered Jorge Mendes 1,623 -
1
votes1
answer53
viewsA: Identify the ID that has value at least one column for all rows
It has to pivot twice. The pivot_longer for the variables of the accounts after the pivot_wider to distribute the years. library(tidyverse) df <- structure(list( id = c(1111, 1111, 1111, 1111,…
-
3
votes1
answer73
viewsA: ggplot2: get color palette used in scale_color
From what I found you can use ggplot_build to assemble the chart information and extract the color from the data. Another option is to use paletteer functions to achieve colors. library(ggplot2)…
-
3
votes2
answers60
viewsA: Add multiple selects in a single dataset
You can also group the data by month and year, take the values you want and then take the repeated columns. The code is less general but is closer to its initial attempt. df <-…
-
3
votes2
answers62
viewsA: Mixed effects model residue plots using ggplot2
Look, by the names (.fitted, . Resid) the results seem to be linked to the Broom package, which uses this pattern for column names. (or more specifically Broom.Mixed for lme models) With the syntax…
-
5
votes1
answer72
viewsA: How to transpose from "wide" to "long" (wide to long) with several variables?
This pivot is a specific case where you have a two variables in the column name. The function pivot_longer can work with it but it was a relatively recent implementation. The last example here…
-
6
votes1
answer42
viewsA: Is it possible to multiply a variable of type "factor"?
A variable of the class factor is a set of values and labels, the levels. This class then serves to store categorical data, making no sense operations like multiplication with it. In…
-
2
votes1
answer49
viewsA: Create column with another column value if conditon is TRUE
The ifelse of R itself exists for exactly this situation, applying an if/Else to a vector in a more compact way. #ifelse aplica um if/else a um vetor ifelse(c(1, 0, 3) > 0, "SIM", "NÃO") #>…
-
2
votes2
answers141
viewsA: Python value and reference
The copy was created when the object m era [linha] * 2, in the case of a repeated list. Mt is a copy but still has a repeated list twice, it just does not share the reference with m. If you change…
-
0
votes2
answers73
viewsA: How to compare snippets of two columns of a Dataframe to generate filter
An alternative is to use an apply to separate the part you want and then compare the two series. df = pd.DataFrame({'Item 1':['carro do joão', 'carro do josé', 'carro do thiago'], 'Item…
pythonanswered Jorge Mendes 1,623 -
0
votes1
answer49
viewsA: Extract attributes from a dataframe using the points of a second dataframe
If I understand correctly I think that’s what you want. Using the sp to create a polygon and seeing in which polygon the point falls. I used the Vignette "Map overlay and Spatial Aggregation in sp"…
-
2
votes2
answers72
viewsA: How to use the rm function without erasing everything, leaving only one or two vectors?
The problem is the environments. Each R function creates an environment for you and both ls how much rm act by default in the current environment. To affect the global environment you have to…
ranswered Jorge Mendes 1,623 -
5
votes2
answers124
viewsA: LANGUAGE R: Error "Error in course[[1:98]] : recursive indexing failed at level 2" when trying to search the items in a list, how do I resolve?
[[ ]] only returns individual values, usually if using an integer or a string with it. If you pass a vector it looks recursively. X[[1:2]] is the same as X[[1]][[2]]. That’s why he gave his…
-
0
votes2
answers87
viewsA: Update data and add new data
I don’t know a function to update directly in excel but you can build a data.frame updated with a function, and then save it in excel. library(readr) #só para ler as tabelas df1 <-…
ranswered Jorge Mendes 1,623 -
1
votes1
answer41
viewsA: Why when creating a zoo series object the columns change from Numeric or integer to Character?
Your two questions have to do with the format of the zoo class and the operation of matrices in R. A zoo object is an ordered matrix and with this order, in your case the dates, serving as a new…
-
5
votes3
answers1427
viewsA: Moving average in R
The values are correct, the problem is the behavior of R with two vectors of different size. The moving average needs 7 values to be computed, so R only starts the analysis at 7 point and the first…
ranswered Jorge Mendes 1,623 -
4
votes1
answer65
viewsA: Graph with error bar in R
The error line problem as I understood it was by the name of the variable of the central value, fixing it the lines are drawn. The caption problem was the x and y values provided, you were creating…
-
6
votes1
answer82
viewsA: R - Function to generate graphs and change axes
You can use a {{ }} that solves most of your problems. library(tidyverse) library(ggpubr) dados_teste <- select(diamonds, carat, price) gera_graficos <- function(base,var1,var2){ hist1 <-…
-
5
votes1
answer70
viewsA: How to change scales of a multifaceted graph in ggplot2
Use the argument scales of function facet_wrap. suppressPackageStartupMessages({ library(tidyverse) }) url <- httr::GET("https://xx9p7hp1p7.execute-api.us-east-1.amazonaws.com/prod/PortalGeral",…
-
2
votes1
answer71
viewsA: How to order by class and by descending order?
Well, the point is that you used two arguments at once. The .predicate, which only exists in functions with _if is to select variables, so it must have the function that selects them, in case…
-
1
votes1
answer655
viewsA: Reorder the columns of a bar chart and its caption
You can pull the reorder for a mutate, then already fix the bars and subtitles at once. library(tidyverse) mpg %>% group_by(trans) %>% count(trans) %>% ungroup() %>% mutate(trans =…
-
5
votes4
answers101
viewsA: Dealing with dates of heterogeneous formats in R
You can use the parameter tryFormats of function as.POSIXct along with some sort of function apply, then it tests for each value which of the provided formats is appropriate. library(magrittr) x…
-
3
votes3
answers115
viewsA: Regular expression of citations in R
With the model you put on I managed to do so. library(tidyverse) #usando o tidyverse citations <- str_extract_all(line, "@\\w*") %>% as_vector() %>% str_remove("@") citations #> [1]…
-
1
votes1
answer146
viewsA: Transpose column to row with condition in r
Here using the tidyr and dplyr of tidyverse and creating variables to use index. library(tidyverse) df_final <- df %>% #Numera os grupos para cada número isolado em df mutate(Contagem =…
-
1
votes3
answers55
viewsA: Convert a factor (of real numbers) into a numerical vector
You can use parse_number package readr of tidyverse. Is a as.numeric more flexible. library(tidyverse) df %>% mutate(var = parse_number(as.character(confianca), locale = locale(decimal_mark =…
ranswered Jorge Mendes 1,623 -
7
votes2
answers182
viewsA: How to use filter() to select only a part of the string?
The %in%serves only to search for element. It will look for elements that are exactly equal to some element of a vector. In your case it really just looks for exactly the same strings '*E119'. If…
ranswered Jorge Mendes 1,623 -
0
votes2
answers649
viewsA: Sum of vector in R
Look, from what I understand, you can do everything vectorially anyway. R does vector operations element by element, so instead of making a function that has this behavior, transform the output of…
ranswered Jorge Mendes 1,623 -
2
votes3
answers5543
viewsA: Change axes X and Y graphs ggplot in R
Just giving another option using the great example of Carlos. If you want something more complicated/different changing the axes you can use the package scales, which is what the ggplot uses to…
ranswered Jorge Mendes 1,623 -
2
votes1
answer114
viewsA: Problem with Recommendation System in R
Put an extra clasp on listaRecomendacoes so that it receives the entire list and not only content. There the user Ids are the names of the lists. listaRecomendacoes <- vector('list', usuarios)…
-
1
votes1
answer441
viewsA: Set percentage values in bars in ggplot
Use the geom_text to insert the text and function percent package scales to make the transformation. library(ggplot2) library(scales) SP=data.frame( Setores=c("Extrativa mineral", "Indústria de…
ranswered Jorge Mendes 1,623 -
1
votes2
answers78
viewsA: How to assemble a matrix in R being the values the subtraction of values of a vector
Just think of the matrix as a set of vectors, then you form the rows and columns and join in a matrix. Here already done at once, I create a vector with the concatenated lines and then transform…
-
1
votes1
answer61
viewsA: How to fix error in auto model.Rhyme using Time Series in R?
Your data is in format character and not as numeric. I’ll post it right away by the information you’ve given me. You can also fix this on import by viewing the parameters you used. If you do this…
ranswered Jorge Mendes 1,623 -
2
votes1
answer53
viewsA: Consult Multiple Columns in R
There are several ways. Here one using the function apply, with a for and with the package dplyr. I only posted the first one that I could get with the same data you sent. The others need the…
-
2
votes1
answer399
viewsA: How to perform a prediction using Multivariate Linear Regression model in R?
You have to pass the new data as a data frame as newdata for the function forecast. Being the name of the columns of the data frame has to match the name of the model variables. library(forecast)…
ranswered Jorge Mendes 1,623 -
0
votes2
answers144
viewsA: Error when manually adjusting the axis with ggplot2
I went to see the data on that ivs Ipea (which I did not know thank you for indicating) and saw that it downloads the data as xlsx. When it imports the data, I am assuming that you used the Rstudio…
ranswered Jorge Mendes 1,623 -
3
votes1
answer41
viewsA: End of run message in a Shiny app
You can try using the function showModal(), to display a warning at the end of the step. library(shiny) library(rmarkdown) library(shinythemes) ui <- fluidPage( #tema theme =…
-
1
votes1
answer1875
viewsA: Run "Procv" on a Dataframe Pandas
For data.frames the method for taking repeated data from a column is drop.duplicates(). And sort_values() to leave the values in the ascending order of a column. nova_df =…
-
1
votes1
answer78
viewsA: Error fitdist "should not have NA or Nan values"
A function thatfitdist flame, startarg, error if there is some value equal to zero in the vector inserted in fitdist. if (distr == "weibull") { if (any(x < 0)) stop("values must be positive to…
-
1
votes1
answer93
viewsA: Adding an area shaded over a Plot in the R?
Two things I changed to make the chart. The x-axis has to be in shape datetime and not in date. To stay in the same format as the x-axis of the chart. I used 11:00 as a time, but it serves anyone on…
-
3
votes2
answers84
viewsA: Plot is different from function values
What happens is that your if only gives one answer, TRUE or FALSE, because it reads the whole list at once. As all elements do not meet the conditions the comparison results in FALSE and goes…
ranswered Jorge Mendes 1,623 -
2
votes1
answer75
viewsA: How to plot only a part of a Posixct variable?
Look, the graph that you sent the image is in minutes no? If it goes from 0 to 9 minutes the graph shows from 00:00 to 09:00, being minutes:seconds. But anyway, if you want to make sure it’s for…
-
2
votes1
answer40
viewsA: Apply function, extract regression data
The problem is that your list model_list has elements of the type list, which are the result of lm, NULL type elements, where the if did nothing, and type NA elements, where she passed to the else.…
-
4
votes4
answers1763
viewsA: How to know which is the largest variable of a vector in R?
R does not automatically name vectors, ideally you turn it into a data frame of a line. vetor <- data.frame(x,y,z) Then you can make use which.max, that returns the position/variable with the…