Posts by Alexandre Sanches • 1,223 points
58 posts
-
5
votes3
answers102
viewsQ: How to do linear interpolation on R?
I have a date.frame with 3 columns: YEAR, COHORT and Income. I would like to make a linear interpolation between the values of 1960 and 1980 to define the values of 1970. To the COHORT = 5, would…
rasked Alexandre Sanches 1,223 -
2
votes2
answers39
viewsA: Using If in R in a data frame where the column is string
The error is occurring because you have not placed parentheses after the if and else if. The right thing would be: if (populacao$UF %in% ("AM", "AP", "AC", "RR", "PA", "RO", "TO")) {…
ranswered Alexandre Sanches 1,223 -
2
votes1
answer26
viewsQ: How to make a filter based on a condition?
I have a date.frame with two columns (CLASSWK and IND) and I’d like to apply a filter based on one condition, but I’m unable to do that. I have a column CLASSWK, that I only want to keep values that…
-
1
votes2
answers43
viewsQ: Left_join returning dataframe with more lines than the original
I have a date.frame with IBGE census data and a column called IND and I have another date.frame with a column IND and another column NEW_IND. The column NEW_IND contains the new codes for the…
-
-2
votes1
answer46
viewsQ: Hide part of a chart caption
I have a chart made with ggplot2, in the legend is appearing a size and I’d like to remove it, but I don’t know how. My code: updates %>% mutate(mortalidade = deaths / confirmed) %>%…
-
1
votes2
answers55
viewsQ: Transform dataframe from long format to wide
I have a dataframe in long format and would like to transform it to wide format for exporting to Excel. I am using the function pivot_wider package tidyr, but the values are coming out this way: Man…
-
0
votes1
answer62
viewsA: Rvest is returning the following error when reading a page: {xml_nodeset (0)}, how to resolve?
It was not clear in the question what you are looking for on the page, I believe the bills are. Normally, tables in HTML use the class = table. Behind the scenes I think the function read_html looks…
-
0
votes1
answer226
viewsA: Problems plotting graph in R with ggplot2
Gabriel, it is always important to provide your database to increase the chances of receiving a response. Observing your code, the error is in the form you wrote. Instead of:…
-
3
votes1
answer35
viewsQ: How to check the periodicity of a series in the R?
I have a database with 5182 series and I need to check if these are daily, monthly or quarterly. Doing this job manually would take a lot of time, so my question is: It is possible to check whether…
-
3
votes1
answer60
viewsQ: Geom_rect does not recognize alpha
I have a chart made with geom_rect, the problem is I’m using the argument alpha and he’s not applying the transparency on the chart. My code: dados %>% mutate(median_x = inicio + floor((fim -…
-
1
votes4
answers150
viewsQ: Calculate percentage of an item in a group per year in R
I have a DF 14-column: item, grupo and years 2010 to 2021. I need to know the percentage of each item within the grupo to which he belongs. I tried to use a group_by with the summarise, but since I…
-
-1
votes1
answer41
viewsQ: Send an e-mail to R
I’m trying to e-mail through the R with the package mailR. I put the information that the package requests, but testing with different emails, I end up getting the same error. My code:…
-
0
votes1
answer49
viewsA: I need to get my chart in descending order!
It is always good to leave a reproducible example to receive appropriate responses. As stated in the comment, always leave a dput of data. To leave a graph in descending order, you can use the…
ranswered Alexandre Sanches 1,223 -
1
votes2
answers126
viewsQ: Geom_area with different filling colors
I have a DF with 4 columns: data, original, previsto and change. The variable change indicates whether the values of the series original are positive or negative, to plot on the chart in green and…
-
0
votes1
answer116
viewsQ: How to use summarise?
I have a DF with energy data and would like to group the data by activity (because they are divided by state and I want to do the aggregate for Brazil) and add these data for each date. I was trying…
-
2
votes1
answer70
viewsQ: How to plot a time series graph with ggridges?
I’m trying to plot a graph of cases per state with geom_density_ridges package ggridges, to stay that way: But by plotting the graph, it’s getting that way, all with the same line: What am I doing…
-
1
votes2
answers99
viewsA: Remove NA from a csv in R
There are some ways to select the columns you want. I will use two examples, through the package dplyr and by R base. Bundle dplyr: library(tidyverse) dados <-…
-
0
votes1
answer39
viewsQ: Storing data from a for in a dataframe
I have airport codes and needed to store the data access link of these airports in a DF, I tried to do through a for, but is returning error. Code I used: aeroportos <- c("bsb", "cgh", "gru",…
-
0
votes2
answers172
viewsQ: How to remove NA from a dataframe in R?
I have a DF that has some NA, as I will use it to assemble a table, I needed to exchange the NA by " ", to only show the empty space in the table. I am using a mutate_if to leave the numbers to only…
-
2
votes1
answer113
viewsQ: How to specify facet_grid columns in ggplot2?
I’m using Google’s mobility data to compile some charts, I needed to make a 5x5 chart, where in the rows would be the states and in the columns, the locations. I tried to use the facet_grid, but it…
-
1
votes1
answer448
viewsQ: Error reading file . xlsx in R
I have a file .xlsx which I need to read in R. The tab is variable, so I use a code first to check which tab is the most recent, but when using the function read_excel package readxl, returns the…
-
2
votes2
answers81
viewsQ: How to join more than two dataframes in R?
I have a list of 12 DF's that I need to put together in a single DF. The structure of all is equal, only differing the name of the second column, so the basic structure is: Column 1 is the date and…
-
2
votes2
answers110
viewsA: Auto increment in R
Always remember to bring a reproducible example. I created a DF simple as an example, but the concept is the same for others DF's. Code: x <- data.frame(z = c(51:101), y = c(50:100)) for(i in…
ranswered Alexandre Sanches 1,223 -
0
votes0
answers62
viewsQ: How to make a bar graph with ggplot2?
I have a graph generated by a package that would like to replicate it and make some changes to the colors, but I’m not able to understand the code behind the chart. Graph: Code used by the package:…
-
1
votes1
answer48
viewsQ: How to make a double filter on a long-format dataframe
I have a DF in long format with data from several countries. I would like to make a filter to fetch the last available value of the variable total_tests of each country, those without data are…
-
0
votes1
answer56
viewsQ: Graph with ggplot2 using wrong data
I have a bar and line chart made with ggplot2, the red line is a moving average of 7 days, but when plotted on the chart, it shows a drop at the end of each that does not match the values. Graph:…
-
5
votes1
answer101
viewsA: Rmarkdown - How to create a . pdf with a different name than . Rmd?
You can use the function render for this. Remembering that this function cannot stay inside the file .rmd, she has to be inside a file .R. Code render(input = "nomeDoSeuArquivo.Rmd", output_file =…
-
2
votes1
answer44
viewsQ: How to use which.max in a long-format dataframe?
I have a DF in long format and tested the following code: library(tidycovid19) # Pacote do GitHub - https://github.com/joachim-gassen/tidycovid19 library(tidyverse) updates <-…
-
2
votes1
answer1724
viewsQ: How to change the color scale on a chart in R
I have a bar chart of Brazilian states. The color of the state bar is according to the region that it belongs to. The color scale was automatically set by ggplot2, I would like to exchange these…
-
2
votes1
answer69
viewsA: How to put currency prefix (R$) in the status bar of the shinyWidgets R package?
You can use the function dollar_format package scales, being as follows: library(scales) real <- dollar_format(prefix = "R$ ") Just use the function with any value inside that will return with…
-
2
votes1
answer32
viewsQ: How to order one dataframe by order of another?
I have 3 dataframes with 2 columns and 5 rows each, as follows: Dataframe 1: estado casosNovos <chr> <dbl> 1 SP 3189 2 CE 1921 3 AM 1365 4 PA 1135 5 MA 938 I would like to order the DF2…
-
4
votes1
answer145
viewsQ: How to make forecast charts in R?
I have a model that makes predictions and would like to plot the observed series and then the predictions, according to this graph: I tried to use the geom_smooth along with the geom_line, but…
-
3
votes3
answers1427
viewsQ: Moving average in R
I need to make a simple 7-day moving average on R, I’m using the function rollmean package zoo, but the values being returned are incorrect. Example: library(zoo) teste <- sample(1:50) mean <-…
rasked Alexandre Sanches 1,223 -
1
votes1
answer70
viewsQ: How to change scales of a multifaceted graph in ggplot2
I have a graph composed of 27 subplots, each corresponding to a state, some states, such as SP and RJ have values much higher than other states and the scale of the graph is in larger intervals. Is…
-
1
votes1
answer67
viewsA: How to organize dates in ggplot2
You can use the scale_x_date or change the angle of the dates. Using the scale_x_date: scale_x_date(date_labels = "%b %d", date_breaks = "2 week") Changing the angle of dates (in this case by 45…
-
3
votes1
answer438
viewsQ: How to increase Y-axis scale marks in graphics in ggplot2?
I have a chart with very long scale marks between you, I’d like to increase the amount, but I don’t know how it’s possible. I would like to increase the number of values in the Y axis, for example,…
-
3
votes1
answer255
viewsA: How to change bar graph border size in ggplot2
For this just add the argument size in function geom_col. The argument widthchanges the size of the fill, while the size changes the border size. New code: dados %>% mutate(mm7dCasos =…
-
1
votes1
answer255
viewsQ: How to change bar graph border size in ggplot2
I have a bar graph on the R made with the function geom_col. I would like to decrease the border size, because it is not showing the chart fill. I tried using the argument width, but is only…
-
0
votes1
answer67
viewsQ: How to organize dates in ggplot2
I have a chart made with ggplot2, but when it comes to exporting it, the dates on the X-axis are totally unreadable, as in this example: How can I fix them? Code I’m using:…
-
1
votes1
answer114
viewsQ: How to filter data by a text pattern in R
I have a database with data of Brazilian cities and states, I would like to filter only the states, all follow a pattern of having a "br_states_" before the name of the state. I tried to use the…
-
1
votes1
answer71
viewsQ: Seasonality chart with ggplot2
I have an example graph that shows the seasonality of a given per month over the years. I would like to adapt the chart to days of the week, I have the example code, but I’m not able to adapt it.…
-
4
votes1
answer342
viewsQ: Bar graph with different colors in ggplot2
I have a bar graph in the R with percentage variation, I would like the negative percentage values to be red, as in this example: But my chart is getting this way: Code I am using:…
-
2
votes1
answer150
viewsQ: Graph of cases accumulated with ggplot2
I’m trying to make a chart according to the chart below days after 100 confirmed cases, in the case of my chart, I put as days after 10 confirmed cases: I’m using this code: library(tidyverse) dados…
-
2
votes1
answer168
viewsQ: Display data label in line chart in ggplot2
I have a line chart and I would like to put the label of the state corresponding to the line, but the label is coming out on each data available and not only the last one: Code I’m working on:…
-
1
votes1
answer265
viewsQ: Remove "Nan" from a data.frame in R
I have a data.frame with two columns that have some NaN and Inf and I’d like to replace them with NA. I was using this code, but it didn’t work: library(tidyverse) dados <-…
-
2
votes1
answer45
viewsQ: How to make inter-weekly variation on R
I have a database in long format and I need to do the inter-weekly variation, ie today’s data / given 7 days ago - 1. Code I used, but is doing the variation of the previous day: library(readr)…
rasked Alexandre Sanches 1,223 -
1
votes2
answers673
viewsQ: How to sum a column in R
I am using a database with several dates and would like to sum up the values of the last available date, I can filter the data.frame by date, but I can’t make the sum of the values, I’m trying to…
-
2
votes1
answer129
viewsQ: How to put captions on a line chart in ggplo2
I have a line chart with two variables and I need to put the caption of each one, however, I’m not getting. I’m using a code like this: library(ggplot2) data <- c(1:50) dado <- c(1:50) dado1…
-
0
votes0
answers113
viewsQ: Remove PDF file table in R
I have a PDF file that contains a table, I need to remove it and turn it into data.frame. I’m trying to use the package pdftools. I’m trying to use the following code, which ends up returning me to…
-
2
votes1
answer124
viewsQ: Generate a pop-up window in R
I have a code in R that generates an output message stating whether a series is updated or not. I run the code through a file. bat on Windows and it generates a txt file. I tried to generate a…