Posts by Tomás Barcellos • 5,562 points
126 posts
-
3
votes3
answers146
viewsA: Print more variable text
The R does not have this syntax of f-string, but the package Glue offers something similar. With it we could do something like this: x <- 10 glue::glue("seu numero é {x}") #> seu numero é 10…
-
4
votes1
answer37
viewsA: Numeric type, I can’t do a range
Just have an NA in the vector to range back c(NA, NA). The minimum example is this. vetor <- c(1, NA, 3) range(vetor) # [1] NA NA To correct this, use the argument na.rm. Thus range(vetor, na.rm…
ranswered Tomás Barcellos 5,562 -
2
votes1
answer241
viewsA: Confidence interval using ggplot2
To include the confidence interval in the chart you have to first determine them. To show how it is included in the chart I will create random values. df <- data.frame(x, y) media <-…
-
3
votes1
answer113
viewsA: How to specify facet_grid columns in ggplot2?
To use facets in the desired way, the data has to be transformed to a format Tidy (see more here) where metrics are placed in one variable and their measurements in another. The code below does…
-
2
votes1
answer117
viewsA: Starting point of axis X using scale_x_date()
First of all, we will create the data that will be used to reproduce the problem and then to demonstrate the solution. set.seed(123) dados <- data.frame(data = seq(Sys.Date(), by = "-1 year",…
-
4
votes3
answers291
viewsQ: In R, what is a Tibble?
What is a tibble? How she differs from a data.frame? The code below creates a data frame.. set.seed(123) df <- base::data.frame( id = 1:10, texto = letters[1:10], numero = rnorm(10) ) df #> id…
-
4
votes2
answers110
viewsA: Auto increment in R
One way to do this is to use the function rownames_to_column() package Tibble. The example below includes ids for table rows iris. In this case it creates a column called "rowname".…
ranswered Tomás Barcellos 5,562 -
3
votes1
answer160
viewsA: Problems with the geobr package of R
It seems that the municipalities that are not on the map are outside the data.frame used to create the map (final). I would venture that this was the result of a left_join from a table (left) that…
ranswered Tomás Barcellos 5,562 -
1
votes1
answer63
viewsA: How to put a delay inside a function in R?
The function that includes an interval between the execution of one command and another in the R is Sys.sleep(). The argument passed is time, in seconds, that the R must "sleep".…
-
0
votes2
answers58
viewsA: How can I filter the first occurrences of a certain variable in my R data frame?
The dplyr has a very useful function for this: first(). You can use it to return the first occurrence of a vector. Combining it with group_by() we can come up with a solution to the problem: df…
ranswered Tomás Barcellos 5,562 -
4
votes2
answers81
viewsA: ggplot appears empty: data does not appear
As mentioned in the first comment, simply remove the ID and Freq quotes. Then we would have: ggplot(det_id, aes(x = ID, y = Freq)) + geom_bar(stat = "identity") + scale_x_discrete(limits = c("Ana",…
-
4
votes1
answer190
viewsA: Line operations of a Dataframe in R
Two possible ways to solve this problem. First, let’s create the data: set.seed(37) library(dplyr) dados <- tibble::tibble(id = 1:6, valor = rnorm(6, 100, 20)) First: create a variable that puts…
-
4
votes1
answer90
viewsA: What does "foo() <-" mean in R?
Short answer foo(obj) <- valor is the form of Rdefining valor as an attribute foo of the object obj. Long answer This is a very common way of defining some attributes of an object. It is common…
ranswered Tomás Barcellos 5,562 -
0
votes1
answer15
viewsA: Complete values in column based on condition and value in another column
One of the ways to do this is to build a "dictionary" or "jig" with the values you want to unite and then perform a union. The code to create the feedback can be something like this:…
dplyranswered Tomás Barcellos 5,562 -
1
votes1
answer103
viewsA: Error in class(x) <- setdiff(class(x), "pseries") :
Brief explanation (or not so much) Error is in function call plm. Note that the formula, first argument, should reflect the relationship between the variables present in the second argument (data).…
-
5
votes1
answer342
viewsA: Bar graph with different colors in ggplot2
There are two changes that need to be made in the code. Both in the geometry of colUnas. Include within the aes(..., fill = cor); and Remove the attribute fill = "#ADD8E6". What happens is that when…
-
3
votes1
answer81
viewsA: Doubts running ggplot 2: out-of-order data, and error bar
Let’s split the problems. 1) Change the order of the X-axis: First of all, a small change of the data that has the variable name as a factor (that introduces some problems that will not help us…
-
3
votes1
answer265
viewsA: Remove "Nan" from a data.frame in R
The problem is in what way to test for special values like NaN and NA not with the operator ==, but with specific family functions is. So we have the following result using == NA == NA #> [1] NA…
-
4
votes1
answer109
viewsA: Regex - Extract numbers
Positive look Behind The search should make use of the operator "Positive look Behind" (?<=nao_retorna)vai_retornar. This operator will start searching for the regular expression only after…
-
3
votes1
answer45
viewsA: How to make inter-weekly variation on R
You can use the functions dplyr::lag() and base::diff(). The function diff(x, n) will return the difference of the values of n days ago. The function lag(x, n) will return the values of n days ago.…
ranswered Tomás Barcellos 5,562 -
3
votes1
answer47
viewsA: How to turn a table with columns with days and rows with months into a regular time series?
First let’s create an object dados with the raw table data. dados <- read.table(text = "DIA;JAN;FEV;MAR;ABR;MAI;JUN;JUL;AGO;SET;OUT;NOV;DEZ 1;NA;NA;.0;.0;.0;.0;.0;.0;.0;.0;.0;.0…
-
1
votes1
answer54
viewsA: Locking the limit of one widget (Shiny) as a function of another
What you need to do is include an observer in sliders. This can be done with observe() or with observeEvent() (which I recommend in this case). When the observed event occurs what we will do is…
-
5
votes1
answer699
viewsA: Plot of lines with months on the x-axis in ggplot - R
The central in the ggplot are always your data. Your data are in a numerical format and therefore the ggplot believes it is better to use a continuous scale in X and to divide so that the intervals…
-
2
votes1
answer329
viewsA: Basic division in training and testing in R
You can use the functions head and tail to choose the observations. Here is how to reproduce the question data: dados <- tibble::tribble( ~Data, ~Fonte_Férrea, "jan/18", 160.11, "fev/18", NA,…
ranswered Tomás Barcellos 5,562 -
2
votes2
answers80
viewsA: Organisation of the x-axis
The problem is that you’re creating a graph that has a text variable for what should be a numeric variable. And it can’t be any different as you rely on the "name" of the variables, which will…
ranswered Tomás Barcellos 5,562 -
2
votes2
answers123
viewsA: How to increase the number of iterations in R?
Explanation The error occurs because the transformation to Date in the variable Day can generate NAs depending on the locale of your machine. The NA generated on the first two dates (which has…
ranswered Tomás Barcellos 5,562 -
2
votes2
answers123
viewsA: How to turn column into row in dataframe
To unite lines of data frame.different s you can use the rbind package base or the bind_row of dplyr. Then we would have: library(tidyverse) df4 <- bind_rows(df1, df2) df5 <- rbind(df1, df2)…
-
4
votes1
answer77
viewsA: How does it work and how to use the Reprex function of R?
What is the reprex The reprex is the English nickname "reproducible example". The idea of the package and the function reprex is to run the code in a "new" and "clean" environment, just as it will…
-
1
votes1
answer166
viewsA: How to add two captions using geom_sf and geom_raster in ggplot?
Brief explanation about aesthetics in ggplot2 The ggplot sees aesthetics from two possible points of view. In the first of them an aesthetic - say a color - includes information (in which case it is…
-
3
votes1
answer58
viewsA: Doubt with loop operations
I will not deal with loop 1 and 2 because they work and also because they are the same thing with the difference that one uses the variable vars and the other calculates it on the fly (at the…
-
1
votes1
answer558
viewsA: How to load an Oracle database into R?
For me what worked was to use the RJDBC. The arrangement I’m proposing uses two files in the folder. One with the jar driver and other text file to hide the source code access credentials. The first…
-
2
votes2
answers1298
viewsA: Date and Time in the same field, how to separate in R?
Another alternative is to use the package lubridate, that is part of the tidyverse. Then we would have: library(lubridate) x <- "23:02:1989 14:22" data_hora <- dmy_hm(x) class(data_hora) [1]…
ranswered Tomás Barcellos 5,562 -
2
votes1
answer495
viewsA: Create an executable in R through the Shiny package
Other Windows users can run R through the installation done on another machine if they have access to folder with R executables. With this you just create an executable file (batch) with a content…
-
0
votes1
answer110
viewsA: Percentile Ranking - R
To create the 20 percentiles equally spaced we can use the function seq(). After that we can use the function quantile() to say the values relative to the percentiles we define. percentis <-…
ranswered Tomás Barcellos 5,562 -
1
votes1
answer133
viewsA: error when importing . ODS in R
To read ". ods" files just use the function read_ods package readODS. In the example below I create an ODS file with the data from data frame. mtcars and then I read it. tf <- tempfile(fileext =…
-
1
votes1
answer68
viewsA: grouby of cells of a dataframe returns Error in Eval (substitute (expr), send, Enclos): non-numerical argument for binary operator
That seems to solve the problem: txt <- "Data .STOXXR 2000-01-03 478.52 2000-01-04 459.53 2000-01-05 448.19 2000-01-06 446.24 2000-02-07 455.80 2000-02-10 462.97 " library(tidyverse)…
-
1
votes1
answer47
viewsA: How to consolidate data set with dplyr?
To transform the values into columns, it is possible to use the function spread() package tidyr. See more about these transformations here. sinan_count <- tibble::tribble( ~agente1, ~regiao, ~n,…
-
2
votes1
answer265
viewsA: make Sankey diagram with two-way links R
The package ggforce can help you in this mission. library(tidyverse) library(ggforce) tidy_landuse <- landuse$links %>% gather_set_data(1:3) ggplot(tidy_landuse, aes(x, id = id, split = y,…
-
4
votes1
answer50
viewsA: Join scattered values on different lines in the same line
The tidyr has the function unite() that can be used for this. df %>% tidyr::unite(msg, msgA:msgC, sep = ", ") obs msg 1 1 A, NA, NA 2 1 NA, B, NA 3 1 NA, NA, C 4 2 A, NA, NA It turns out that the…
ranswered Tomás Barcellos 5,562 -
4
votes1
answer907
viewsA: When to use the.call function?
The do.call should be used when you want to pass a list of arguments to a function as opposed to passing a list as argument to the function. That is, to call do.call(rbind, lista) is the same as…
ranswered Tomás Barcellos 5,562 -
2
votes2
answers250
viewsA: How to select strings that start with a given word
For regular expression to only find at the beginning of the line use ^ at the beginning of the expression. So it is possible: library(tidyverse) ES_1_3 <- data_frame( Pathways =…
-
2
votes1
answer33
viewsA: Reading Rdatas returning dates frames with different names
There are a few possible solutions. The most recommended is to stop using files .RData and use aquives .rds because they don’t change the environment and help make problems reproducible. First of…
ranswered Tomás Barcellos 5,562 -
3
votes2
answers327
viewsA: How to identify a single point and label it in R
First let’s create a dataset to use in the answer. I used the package datapasta to copy and paste the table of this page. library(tidyverse) pibs <- tibble( posicao = c(1, 2, 3, 7, 4, 5, 6, 8, 9,…
ranswered Tomás Barcellos 5,562 -
4
votes1
answer423
viewsA: What is the use of arrays in r?
An array, in the R, is a generalized form of the matrix. An array has only one dimension: size. The matrix has only two dimensions: rows and columns. An array can be n dimensions. Vector case…
-
2
votes1
answer72
viewsA: Export file . SAP in R
The package Haven provides two ways to export file to the SAS in the R. These two shapes are packaged in functions write_sas() and write_xpt(). You can read in their documentation which form is best…
-
5
votes1
answer1144
viewsQ: How to train a decision tree in R?
Let’s say I have the following data sets. set.seed(123) n <- nrow(iris) indices <- sample(n, n * 0.8) treino <- iris[indices, ] teste <- iris[-indices, ] How could I use the R to train a…
-
6
votes7
answers1523
viewsA: How to calculate the median of a line in a date.frame in R?
A little of the explanation is here. Reproducing the data library(tidyverse) txt <- "Linha A B C D E L1 4 3 2 2 4 L2 1 11 1 1 1 L3 0 1 2 3 4 L4 2 0 0 8 0" DADOS <- as_tibble( read.table(text =…
-
4
votes1
answer393
viewsA: Caption in ggplot does not appear
ggplot is not putting subtitles automatically because, the way you’re doing the graph, he thinks there’s no need. By creating each geometry you are specifying color as an aesthetic attribute and not…
-
3
votes3
answers673
viewsA: How to go through the data.frame cases using `dplyr`?
The mistake The error message indicates that the function apply(), call for f2() is being rotated into an object that does not have two dimensions. This is because the mutate will try to apply the…
ranswered Tomás Barcellos 5,562 -
4
votes1
answer223
viewsA: Widget in Shiny to take a file path
The Shiny in fact does not have a function for it. To circumvent this "restriction" of shiny the package was created shinyFiles. This package has two functions to assist in this mission:…