Posts by Daniel Falbel • 12,504 points
268 posts
-
1
votes1
answer104
viewsQ: How to convert a tuple to a`void`pointer vector?
I have tuples with objects that can have different types. How I would generalize the following code: #include <iostream> std::vector<void *> to_vector(std::tuple<int, double,…
c++asked Daniel Falbel 12,504 -
2
votes1
answer59
viewsQ: What happens when using namespace within a namespace?
What happens when I use the directive using namespace x within a namespace? For example: namespace x { int k = 1; } namespace y { using namespace x; } Now when I use the namespace y can access…
-
2
votes1
answer34
viewsQ: Convert Std::vector p/ Std::array implicitly?
How do p/ extend the std::vector so that there is an implicit conversion of std::array for std::vector? Imagine I have this: template <class T, int N> std::array<T, N> vector_to_array…
c++asked Daniel Falbel 12,504 -
2
votes1
answer244
viewsA: Annual percentage change in R with dplyr
You don’t have to group_by in this case: library(dplyr) df <- data.frame(ano = 2001:2010, indice = runif(10)) df %>% mutate(variacao = (indice/lag(indice, 1, order_by = ano) - 1)*100) #>…
ranswered Daniel Falbel 12,504 -
8
votes1
answer204
viewsA: Download zip files from a link to a PC folder via R
You can do so by using the package curl: library(curl) library(readr) url_opcoes <- "ftp://ftp.bmf.com.br/MarketData/Bovespa-Opcoes/" con <- curl(url_opcoes) arquivos <- read_delim(con,…
ranswered Daniel Falbel 12,504 -
3
votes1
answer198
viewsA: Difference in Main Component Analysis (PCA) graphs
The function autoplot of ggfortify makes a kind of standardization. More specifically does the following: library(ggplot2) library(ggfortify) iris.pca <- prcomp(iris[, -5]) x <-…
-
3
votes1
answer23
viewsA: Extract only value from the hypothesis test for an object
The result of ur.kpss is a class object S4. To extract elements we use the operator @: library(forecast) library(ggplot2) library(urca) library(lmtest) #> Loading required package: zoo #>…
ranswered Daniel Falbel 12,504 -
1
votes1
answer151
viewsA: Validating a model with cross validation (logistic regression). What is Grid search for?
In logistic regression there are no hyperparameters for join unless it is a logistical regression with regularisation. Another thing that might be tunada in logistic regression are the variables…
machine-learninganswered Daniel Falbel 12,504 -
3
votes2
answers810
viewsA: Convert all columns of a data frame
You can use the mutate_all: For example: library(tidyverse) #> Warning: package 'tibble' was built under R version 3.5.2 mtcars <- as_tibble(mtcars) mtcars %>% mutate_all(as.character)…
-
5
votes2
answers327
viewsA: How to identify a single point and label it in R
The gghighlight is a sensational package to do this: Here’s an example of how to do with it: library(tidyverse) library(gghighlight) ggplot(pibs, aes(x = posicao, y = valor)) + geom_point(color =…
ranswered Daniel Falbel 12,504 -
3
votes2
answers308
viewsA: Replace numeric values of one vector with another value in a data frame
With dplyr you can do something like this: library(tidyverse) #> Warning: package 'tibble' was built under R version 3.5.2 index_1 <- sample(mtcars$gear, 5) index_2 <- sample(mtcars$hp, 5)…
ranswered Daniel Falbel 12,504 -
5
votes7
answers1523
viewsA: How to calculate the median of a line in a date.frame in R?
An alternative that has emerged recently is to use the package rap: library(tidyverse) #> Warning: package 'tibble' was built under R version 3.5.2 library(rap) txt <- "Linha A B C D E L1 4 3…
-
2
votes1
answer40
viewsA: How to calculate the ROC curve using only confusion matrix
Not possible. The ROC curve is calculated by making a confusion matrix for each possible cut-off point of a score continuous.
-
6
votes1
answer360
viewsA: What is the use of the functions with underline (_) at the end?
The reason these functions still exist today is historical. The dplyr was thinking about who program interactively, so it provides some facilities to those who are programming as: not having to use…
-
7
votes1
answer181
viewsA: How to parallelize on multiple levels in R?
Generally, it doesn’t pay to parallelize at more than one level. This is until it is possible but will not make your code run faster, unless the first level of parallelism is failing to utilize the…
-
9
votes1
answer191
viewsQ: What does the [&] operator mean before a function?
I’m trying to read the code of a function that’s defined like this: auto loop = [&](int ntensor, char** data, const int64_t* strides, int64_t n) { ... }; What does the [&] before function?…
-
5
votes1
answer159
viewsA: How can I add the identical values in the column of a data frame?
You can do something like this: df <- data.frame(x = c(1,1,2,2,2,3,3,3,4,4, NA)) df x 1 1 2 1 3 2 4 2 5 2 6 3 7 3 8 3 9 4 10 4 11 NA library(dplyr) df %>% group_by(x) %>% summarise(sum(x))…
ranswered Daniel Falbel 12,504 -
1
votes1
answer203
viewsA: R connection to Myqsl
R (fortunately) will not load all database tables into memory. If your connection is ok you can use the function dbListTables() package DBI to list all tables. To pull to memory can use: dbGetTable…
-
4
votes3
answers969
viewsA: Differences and similarities between apply and for loop functions
lapply vs for lapply and for are primitive functions in R. Yes, for is a primitive function too: `for`(i, 1:10, {print(i + 1)}) [1] 1 [1] 2 [1] 3 [1] 4 [1] 5 [1] 6 [1] 7 [1] 8 [1] 9 [1] 10 for…
-
4
votes1
answer194
viewsA: My HSD.test does not work. Any tips on how to run?
If you want the output to appear on the console you need to put the argument console = TRUE. For example: > library(agricolae) > data(sweetpotato) > model<-aov(yield~virus,…
-
7
votes1
answer161
viewsA: Difference between Function Factory and closure
That’s right. Consider the example: power <- function(exponent) { function(x) { x ^ exponent } } square <- power(2) In this case the function power is a Function Factory and square is a…
-
5
votes3
answers155
viewsA: Error with variable naming functions in a list
The problem is that neither the function lapply nor the function map allow modifications in-place of objects. When you use the assignment operator <- on the right side of a lapply he doesn’t know…
ranswered Daniel Falbel 12,504 -
10
votes1
answer261
viewsA: What is the usefulness of lexical and dynamic scopes in R?
These concepts are very important for those who develop R packages and need to create complex functions behaviors, although for the day-to-day analysis of data is not so necessary. First, to…
ranswered Daniel Falbel 12,504 -
6
votes1
answer202
viewsA: Functional programming: applicability of parameters (.x) and (.) in purrr::map
The functions of purrr have the following syntax: map(vetor_ou_lista, funcao) Then what he does is apply the funcao for each element of vetor_ou_lista. funcao can be any R function or an anonymous…
-
6
votes1
answer60
viewsA: How to implement mappers in R?
A mapper is a function that relates elements of a set to elements of the same or another set. This is a mathematical concept. To better understand what a mapper in the context of programming we…
-
4
votes1
answer117
viewsA: Problems to plot graph with chartSeries function of the quantmod package from a data.frame to R
It worked that way for me: library(tidyverse) ibov <- xts( ibov$df.tickers %>% select(-ref.date, -ticker), order.by = ibov$df.tickers$ref.date ) chartSeries(ibov) I also deleted that line from…
ranswered Daniel Falbel 12,504 -
5
votes2
answers1524
viewsA: Change <Chr> to number in R
I would do so: library(tidyverse) dadosarrumados %>% mutate_at(vars(Quantidade, Porcentagem), parse_number) # A tibble: 5 x 5 Região Total `Anos de estudo` Quantidade Porcentagem <chr>…
ranswered Daniel Falbel 12,504 -
8
votes2
answers164
viewsA: Remove all Environment elements containing numbers and uppercase letters
You can do it this way: library(purrr) library(stringr) remover <- ls() %>% keep(~str_detect(.x, "[:digit:]|[:upper:]")) rm(list = remover) The function ls() lists all the variables of the…
ranswered Daniel Falbel 12,504 -
6
votes3
answers488
viewsA: Calculation of Difference between Dates
You can do it this way: library(lubridate) DADOS %>% mutate(diferenca = as.numeric(dmy(DATA_FIM) - dmy(DATA_INICIO))) MATRICULA DATA_INICIO DATA_FIM diferenca 1 111 10/12/2017 10/12/2017 0 2 222…
ranswered Daniel Falbel 12,504 -
5
votes1
answer85
viewsA: What are columns-lists of a data.frame?
List columns or list-Columns are a data structure that can be useful at various times when working with tidyverse. They are mainly used as intermediate structures. They can be used in R-base but you…
-
5
votes1
answer37
viewsA: How to optimize the removal of lines in an array?
R is a vector language and the best way to do that would be something like: filtro1 <- filtro1[filtro1[,31] != 0, ] I think the best place to learn about vectorization is Chapter 3 of R Inferno.…
ranswered Daniel Falbel 12,504 -
5
votes3
answers158
viewsA: Minor Date in a Dataset
Another solution with dplyr is using filter instead of slice. library(tidyverse) library(lubridate) DADOS %>% mutate(DATA = dmy(as.character(DATA))) %>% group_by(MATRICULA) %>% filter(DATA…
ranswered Daniel Falbel 12,504 -
6
votes2
answers498
viewsA: How to make mobile sum in R?
I know two good packages to do this. The zoo (as Rui mentioned in the commentary) and RcppRoll. > zoo::rollsum(1:20, k = 5) [1] 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 >…
-
2
votes1
answer71
viewsA: How does the `dplyr::n()` function know that it is not being called from the global environment?
The function n only works within the dplyr and is part of an internal part of the package that is called Hybrid Evaluation. The full description is here. Hybrid Evaluation is one of the leading…
-
6
votes2
answers214
viewsA: Application of the `assign` function in loops
This happens because the assign modifies the parent environment. In the case of the parent environment, the parent environment is the global environment itself. That’s why variables appear to you.…
-
4
votes2
answers1179
viewsA: Weighted Average in R
I’m not sure I understand it very well. Here’s a solution that calculates by year and state the average of C4 weighted by C3. That’s right? library(tidyverse) dados %>% group_by(c1, c2) %>%…
ranswered Daniel Falbel 12,504 -
2
votes1
answer241
viewsA: Error in View : invalid caption argument
What’s happening here is independent of your database. Remember that the operator %>% causes the object to its left to be used as argument of the function to its right. Therefore, the following…
-
2
votes2
answers85
viewsA: Repeating the subtraction of groups in a data frame for all numerical variables
It is possible to do so with dplyr: bind_rows( df %>% filter(grp == "a"), df %>% filter(grp != "a") %>% group_by(index) %>% mutate_at(vars(starts_with("value")), funs(. - lead(.,…
ranswered Daniel Falbel 12,504 -
8
votes2
answers408
viewsA: Builder and inheritance in R
First, I think this question has been left unanswered so far because these concepts are little (not to say "nothing") important to become a good developer in R (unlike other languages). In my answer…
ranswered Daniel Falbel 12,504 -
5
votes1
answer182
viewsA: Lower Value of a Set of Columns
With dplyr you can do so: library(dplyr) DADOS %>% group_by(GUIA) %>% filter(ORDEM == min(ORDEM)) %>% ungroup() # A tibble: 3 x 3 ORDEM GUIA COR <dbl> <fct> <fct> 1 1 111…
-
4
votes1
answer47
viewsA: Problem organizing a tidyr dataframe in R
From what I understand, you want after the spread, the dates are sorted. I don’t know if this is possible using the function spread. I would wear a arrange right after the spread in the pipeline.…
-
5
votes1
answer69
viewsA: Extract dataframes from lists with dataframes under given criteria in R
You can use the function keep of purrr: library(purrr) df.1.in.list %>% keep(~.x[1,8] %in% c(-2, 2))
ranswered Daniel Falbel 12,504 -
2
votes2
answers151
viewsA: Perform the tapply function for multiple variables on a dataframe (with pairwise.t.test)
I don’t know a solution with tapply but follows a solution in style tidyverse. At the end, you get a table with p values for all the tests we did. library(purrr) library(dplyr) library(tidyr)…
-
1
votes1
answer693
viewsA: Subtitles by GGPLOT2
The attribute legend.key.size changes the distance between the elements a little. See if it fits you: library(ggplot2) ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +…
-
4
votes1
answer156
viewsA: Conditional Operations Error in R
The function readline does not work well in non-interactive use. From the function documentation itself we read: In non-interactive use the result is as if the Response was RETURN and the value is…
-
3
votes1
answer86
viewsA: What is the logic of Function(x) and Function(x,y) in R?
The function lapply takes two arguments - the first is a list (can be a data.frame - since it is also a list) and the second argument is a function that will be applied to each element of the list…
-
9
votes2
answers354
viewsA: Generate sequence in R
I would do so: rep(1:221, each = 2) + c(0, 221)
ranswered Daniel Falbel 12,504 -
4
votes1
answer93
viewsA: Including columns in a dataframe in R using a rule
I’d do it this way, using purrr and dplyr. res <- map2_dfc(df, index, function(x, index) { case_when( x < -2*index ~ -2, x < -1*index ~ -1, x < 1*index ~ 0, x < 2*index ~ 1, TRUE ~ 2…
-
2
votes1
answer748
viewsA: Bar graph name in ggplot2
The values of hjust can be negative for you to do what you need. For example: library(ggplot2) ggplot(iris, aes(x = Species)) + geom_bar() + ggtitle("Titulo") + theme(plot.title = element_text(hjust…
-
2
votes1
answer65
viewsA: Error while executing nls in R - 'Arg' must be NULL or a Character vector
On your last code call: n0 <- nls(Y~MM(x, A, B, C), data = dados_Indice, start = start, control = nls.control(maxiter = 200, tol = 1e-05, minFactor = (1/2)^30), trace = TRUE, na.omit(NA)) The…
ranswered Daniel Falbel 12,504