Posts by Tomás Barcellos • 5,562 points
126 posts
-
4
votes2
answers117
viewsA: turn numbers into relative frequency
Reproducing the data dados <- structure( list( X = c("Ver_suj", "Ver_obj", "Substantivo", "Adjetivo" ), Bolsonaro = c(59L, 299L, 988L, 653L), Ciro = c(188L, 242L, 128L, 212L), Manuela = c(59L,…
-
2
votes1
answer254
viewsA: How to make the difference between two data.frames in R?
Within the tidyverse, the package dplyr presents the solution of anti_join(). First, let’s reproduce the data, txt1 <- "CODE A B C D E COUNT 001 0 0 1 1 0 2 002 1 1 1 1 1 5 003 0 1 0 1 0 2 004 1…
ranswered Tomás Barcellos 5,562 -
5
votes4
answers315
viewsA: How to apply several functions to the same object?
Within the tidyverse it is possible invoke various functions with invoke_map() and its variants. The invoke_map has three basic components: .f: A list of the functions (the same objects and not…
-
1
votes1
answer276
viewsA: Download from automatic download link and save changing directory in R
The error was happening because the content of destino was a folder and not the name of a file. Change destino for destino <- paste0(dir_base, pasta, "/download.pdf") destino # [1]…
-
7
votes4
answers315
viewsQ: How to apply several functions to the same object?
How is it possible, in the R, apply several functions to the same object? Example: Let’s say I have a vector x. set.seed(123) x <- rnorm(10) x # [1] -0.56047565 -0.23017749 1.55870831 0.07050839…
-
9
votes2
answers1438
viewsQ: What is wide/long data?
It is common to read/hear comments saying that the data is in format wide or long. Which means a table is in format wide? And long? Which packages/functions in R can be used to transform a table…
rasked Tomás Barcellos 5,562 -
5
votes2
answers812
viewsA: How to count the number of frequencies for each column in a date.frame in R?
First of all, let’s play back the data: txt <- "USUARIO jan fev mar abr mai jun jul ago set out nov dez 1160 0 1 1 1 1 1 1 1 1 1 1 1 2505 1 1 1 1 1 0 1 0 1 1 1 0 3042 1 1 1 0 0 0 1 1 1 1 1 0 3554…
ranswered Tomás Barcellos 5,562 -
8
votes2
answers292
viewsA: Cumulative count of group occurrences on dates
Within the tidyverse there is the function tidyr::complete() to accomplish the desired. The steps to this are: Count the occurrence of each group on each date with count(). Complete with 0 (zero)…
-
5
votes3
answers2296
viewsA: How to format a "date" column inside of a data.frame in R?
First of all, let’s create a reproducible example. library(tidyverse) tabela <- data_frame(timestamp = Sys.time()) And then it is possible to solve the first question like this: tabela %>%…
-
3
votes2
answers49
viewsA: Redeem results in r
The p-value does not exist in the summary object. What the summary object offers are the conditions of calculating the p-value, with the statistic F and the two degrees of freedom. reg <- lm(mpg…
-
4
votes2
answers300
viewsA: Difference between "Function Operator" and "Function Factory"
Ailton is right when he says operator is a particular factory case. But I’ll illustrate this idea better. There are factories of functions that can be of different types, I will show factories that…
-
3
votes2
answers689
viewsA: How to create Time Series using Start and End in R?
The function ts() has 4 main arguments. The first of them are the data that will be transformed into time series. The according to and the third are the initial and final period of the series. Note…
-
5
votes4
answers4136
viewsA: Count equal values in one data frame and store in another in R
The very function count() can accept the name of a column as argument and it then counts the unique values of that column. This way you can simplify the solution offered by @Marcusnunes.…
ranswered Tomás Barcellos 5,562 -
3
votes1
answer104
viewsA: Manipulation of columns-list
The manipulation of columns-list within the tidyverse occurs in the same way that this universe proposes to manipulate lists, that is, with purrr. The difference is that this manipulation takes…
-
4
votes1
answer842
viewsA: How to summarize data in R?
The dplyr works like this. You group the data by your unit of analysis, in case USUARIO and then create a summary of the data for that analysis unit. In this case we have library(dplyr) dados %>%…
-
4
votes1
answer230
viewsA: How to send outliers with ggplot + geom_boxplot?
First of all I’m going to recreate the data with a seed so that the results can be reproduced. library(tidyverse) set.seed(123) dataset <- as_tibble(matrix(rnorm(6*1000,1500,200),ncol=6)) cluster…
-
9
votes1
answer183
viewsQ: What is a Shiny app?
It is common to have to expose a data analysis in a more user-friendly format. In these cases it is also common to hear as a suggestion that this analysis be transformed into a shiny application.…
-
4
votes4
answers145
viewsA: How to count strings from a variable
One way is to count the character "/" and then add one, since the first party is never preceded by the bar. library(tidyverse) dado <- tibble( DS_COMPOSICAO_DA_COLIGACAO = c( "AVANTE / PDT / PODE…
ranswered Tomás Barcellos 5,562 -
4
votes1
answer526
viewsA: Add error bar on graph in R
The code below uses new data, since the original data was not provided in the question. time <- 3:10 tq1_mean <- 3:10 sd <- sd(tq1_mean) plot (time, tq1_mean, type ="p", col = "red",…
-
3
votes3
answers969
viewsA: Differences and similarities between apply and for loop functions
Similarities The family apply is a loop hidden. The code below seeks to syntactically reconstruct the similarity between the lapply and a for. resultado <- vector("integer", 10) for (i in…
-
5
votes1
answer86
viewsA: How to store table names in a Vector in R
Table names can be accessed with the function names(). nomes <- names(Tabela) nomes # [1] "Carro" "preço" "cor"
ranswered Tomás Barcellos 5,562 -
3
votes2
answers72
viewsA: Function `Broom::Tidy` does not produce the output for some functions
The function tidy package broom is a generic function that implements a method for each type of test/model. Therefore, it expects a input with class htest to make tidy a Shapiro test. When you…
ranswered Tomás Barcellos 5,562 -
3
votes1
answer80
viewsA: Error in R with list2env function
As noted by @Ruibarradas, the list should be named that can be transformed into environment. Reproducing the error: mylist <- list(1, 2, "a") list2env(mylist,envir=.GlobalEnv) # Error in…
ranswered Tomás Barcellos 5,562 -
3
votes1
answer647
viewsA: How to deal with multiple quotes in R?
In a sentence: to use multiple quotes in R, escapes the quotation marks of the text or wraps the text in quotation marks other than the quotation marks used in the text. In more detail The r does…
-
1
votes1
answer384
viewsA: Error: In Ops.factor
As pointed out in comments by @Ruibarradas, the error message is stating that this operation is not significant for factors. In the r, factors are categorical variables. And indeed there is no…
ranswered Tomás Barcellos 5,562 -
5
votes1
answer60
viewsQ: How to implement mappers in R?
I would like the answer to contain information on: What are mappers? How to create a mapper on R? In what situations mappers are used (their role in functional programming)?…
-
4
votes3
answers186
viewsA: Remove Environment (and list) elements based on their classes
Based on this reply, it is also possible to do so (more "tidyverse"): library(purrr) remover <- ls() %>% map(get) %>% map(class) %>% map_lgl(~.x %in% c("list", "integer")) %>% keep(.x…
ranswered Tomás Barcellos 5,562 -
3
votes1
answer1089
viewsA: Graph by ggplot in Rstudio v 1.1.463 does not read Subtitle and caption in Labs()
The argument caption was inserted into the ggplot2 version 2.2, which is November 2016 (source). According to some comments, it is not a mistake in Rstudio. Most likely, without knowing the…
-
5
votes3
answers186
viewsA: Remove Environment (and list) elements based on their classes
One option is to use the function is() which is more generic than is.integer, for example. It requires, as the second argument, the class that will be checked. Put the result of mget(ls()) proposed…
ranswered Tomás Barcellos 5,562 -
5
votes1
answer85
viewsQ: What are columns-lists of a data.frame?
The tidyverse stimulates the use of columns-list in data frames.. But, after all, what are columns-list? on what occasions they are commonly used? they can be created with the r-base or just as…
-
2
votes1
answer145
viewsA: Import table in R without names (skip line did not work)
In a sentence As indicated in the comments, this is a typo and it can be corrected by changing the argument starRow (without the t) for startRow. tipo4 <- xlsx::read.xlsx("meusdados.xlsx",…
-
2
votes1
answer51
viewsA: Histogram with a text column
The histogram is a graph designed to visualize the distribution of numerical variables by the sampling space. You can fulfill the same function with categorical information (as should be the…
ranswered Tomás Barcellos 5,562 -
4
votes2
answers498
viewsA: How to make mobile sum in R?
There are a few ways you can calculate the moving sum in the r: R-base diff(c(0, cumsum(1:10)), 5) # 15 20 25 30 35 40 This proposal can be generalized as a function: soma_movel <- function(x, n)…
-
5
votes1
answer49
viewsA: Command to describe table data
The r-base offers the options of: names(): to know the names of the variables that are in the data frame.. str(): to know the structure of the object in question. In this case data.frame, this means…
-
3
votes2
answers66
viewsA: read txt file with less than 5 elements using read.table
Reproducing the problem: tf <- tempfile() write.table( "+-----------------------------------------------------------------------------+ | Category Information | square| | #|description | miles|…
-
2
votes1
answer71
viewsQ: How does the `dplyr::n()` function know that it is not being called from the global environment?
When calling the function dplyr::n() in the global environment, an error occurs. n() # Error: This function should not be called directly This error makes sense and I was curious to see how it was…
-
1
votes1
answer241
viewsA: Draw lines from a specific point and angle
Since the retars are composed of two parts, the challenge is to define them. These two parts are, the intercept and the slope (which are also the aes() required for the geom_abline()). The first…
-
2
votes1
answer55
viewsA: Transform level into variable
Since the SIDRA API was unstable or very slow, I made this small change to the code (I removed the municipalities from the query) to have similar data to work with. library(sidrar) Tab1612SojaRend…
-
3
votes2
answers222
viewsA: reshape2::melt counter function (unstack)
Using the tidyverse, you first need to create a data frame. by aggregating the values of LUX and then spread it (spread()): library(tidyverse) dados %>% group_by(AVA, FAT1, FAT2) %>%…
-
1
votes1
answer57
viewsA: Temporal matching of financial market data downloaded by Batchgetsymbols package in R
Using the tidyverse, an option is to group by date and keep only those observations whose group has more than two members library(tidyverse) ativos[[2]] %>% group_by(ref.date) %>% filter(n()…
ranswered Tomás Barcellos 5,562 -
3
votes1
answer123
viewsA: How to apply the `Join` (dplyr) function in a list?
In the tidyverse, the package used for handling lists is the purrr. To solve the proposed problem we will have to use this package together with the dplyr. First, let me propose a code change to…
ranswered Tomás Barcellos 5,562 -
13
votes2
answers823
viewsA: What is the difference between [] and [[]] in the R?
In addition to Rui’s comments, I share the metaphor created in the book R for data science: Imagine that saleiro be it: So saleiro[1], results in: And, in turn, with saleiro[[1]] we have: That is,…
ranswered Tomás Barcellos 5,562 -
5
votes2
answers408
viewsA: Builder and inheritance in R
The r has at least 4 object guidance systems (OO): S3, S4, Reference Classes (also known as R5 or RC) and R6. The latter system is not part of the r-base, but has been widely used by the community.…
ranswered Tomás Barcellos 5,562 -
21
votes1
answer2245
viewsA: R is an object-oriented language?
The phrase quoted above from Soen is correct. According to John Chambers, creator of r, Everything in R is an object. Everything that happens in R is called a function. This creates the curious…
-
3
votes1
answer304
viewsA: Reading data from PNAD 2016
There is a package created to facilitate the download and reading of microdata from Brazil. The package is called microdadosBrasil and still not in the CRAN. To lower it rotate: # Precisa ter o…
ranswered Tomás Barcellos 5,562 -
10
votes1
answer323
viewsA: How to convert a Shiny app, consisting of multiple files, into a playable example that can be shared in a question?
Broad lines The general lines or principles that will guide the construction of the question are the same listed here, here and in the link quoted in the question. What will change in the Shiny is…
-
4
votes1
answer95
viewsQ: How to use dplyr within a function?
Let’s say I wanted to create a function that internally uses some functions of dplyr or any package from tidyverse using this kind of syntax. For illustration purposes: exemplo <- function(df,…
-
3
votes1
answer609
viewsA: How to insert caption into ggplot maps?
The ggplot is not including the legend because you are assigning colors as attribute and not as mapping. |Set attributes does not include new information in the chart (map), only changes visual…
-
1
votes1
answer202
viewsA: The graph doesn’t appear when I put Shiny to run
To reproduce the problem I created data as follows: dados <- data_frame( Estado = c("Acre" , "Alagoas", "Amapá", "Amazonas", "Bahia", "Ceará"), Sigla = NA, Código = NA, X9 = NA, # código vai…
-
1
votes1
answer119
viewsA: Error installing R packages in Gitlab CI
When trying to rotate the part of R from the code you shared (below), I got a syntax error. This is because in the files *.yml indentation is important. The job hasn’t even started. test: script: -…