Posts by Daniel Falbel • 12,504 points
268 posts
-
1
votes2
answers158
viewsA: Error using package plm - Error in x[, ! na.check] : (subscript) Logical subscript Too long
The first problem is: the package receives a data.frame as input. This can be found in the documentation. date to date., When you are adjusting the model with the matrix, it gives this error. When…
ranswered Daniel Falbel 12,504 -
4
votes2
answers94
viewsA: Organize Results Kalman Filter in a Matrix
If you have a function that takes an array as input and returns another vector as output you can always use the apply. This is for any function. For example, starting very simple. Consider the…
-
7
votes1
answer673
viewsA: How to correctly identify clusters using kmeans?
My first thought is that the kmeans is not a classification method but a Clusterization method. The difference is subtle, but rather important. The kmeans is an unsupervised method. There is nothing…
-
2
votes1
answer108
viewsA: Text Cleaning and Standardization
There’s no magical way to do this. I’m going to propose a way that maybe can speed up your manual work, because at some point you’ll have to check and see which are well ranked or not. With the…
-
1
votes2
answers64
viewsA: R - match and replace string
I’d do it this way: library(magrittr) library(stringr) antes_parenteses <- n %>% str_extract_all(".{1,}\\(") %>% str_replace_all(fixed(" ("), "") parenteses <- n %>% unlist() %>%…
ranswered Daniel Falbel 12,504 -
3
votes1
answer625
viewsA: Graph of columns of a Data Frame using GGPLOT
The following code should do what you need: df_long <- plyr::ldply(1:5, function(i){ aux <- df[,(2*i -1):(2*i)] names(aux) <- c("valor_letra", "valor_k") aux$letra <- LETTERS[i] aux$K…
-
3
votes1
answer709
viewsA: How to replace values with NA using element encoding (not position)?
Use the command %in% in comparison: filhos <- c(1, 3, 5, 6, 8, 10, 11, 12, 15) peso <- c(55, 58, 76, 42, 68, 80, 47, 55, 85) dados <- as.data.frame(cbind(filhos, peso)) set <- c(3, 6,…
-
1
votes3
answers105
viewsA: Changing charts of a regression
Here’s a try to copy this chart: fit1 <- rq(y ~ x, tau=seq(0.05, 0.95, by = 0.05)) library(ggplot2) library(dplyr) library(broom) broom::tidy(fit1) %>% filter(term == "x") %>% mutate(shape…
-
2
votes2
answers173
viewsA: Compare lines between two tables of data?
I don’t know if you need the indexes. If a data.frame with the common lines is enough, an alternative solution is to use the inner_join: > q1 <- data.frame(COD = 1:5, CLAS=letters[1:5],…
ranswered Daniel Falbel 12,504 -
3
votes1
answer101
viewsA: Is there any way to eliminate duplicate elements that aren’t exactly the same?
I made a function that can help you. It uses the package stringdist that calculates the distance between strings: combinar_textos_parecidos <- function(x, max_dist){ x <- as.character(x)…
ranswered Daniel Falbel 12,504 -
1
votes2
answers2571
viewsA: Bar graph - ggplot2
Your mistake happens because the ggplot was designed to work with long format databases. Where each line is an observation. In your case the data is already aggregated by CID. Therefore, you need to…
-
1
votes3
answers126
viewsA: Organize data in excel
Read straight from Excel using the package readxl read_excel("arquivo.xlsx") You can still use the argument sheet if your spreadsheet has multiple tabs. Read the help using ?read_excel. And before…
-
4
votes1
answer370
viewsA: Error as.POSIXlt.Character when importing MDB into R
Try to use the argument as.is = T. JE <- sqlFetch(channel, sqtable="Table", colnames = FALSE, rownames = FALSE, as.is = T) Then you can convert to date using as.Date.…
-
1
votes1
answer216
viewsA: How to make maps in R with parallel processing?
There is probably some map that is giving problem when rotating. Only then you end up losing what you have already done. I recommend you use a function that does not stop the execution when you have…
ranswered Daniel Falbel 12,504 -
4
votes1
answer3912
viewsA: Conversion of data factor to Numeric
In the R the decimal separator is .. For your code to work you have two options: Use the following code to convert factor in numerical: as.numeric(gsub(",", ".", as.character(rhm$wt))) The function…
ranswered Daniel Falbel 12,504 -
14
votes1
answer2490
viewsA: Pattern recognition
First, it’s cool to say that this is a famous machine-Learning problem. It’s available as kaggle challenge, from where it is also possible to download the database. Inclusive, that’s where I…
-
1
votes1
answer38
viewsA: Transform class within package
The following roxygen2 header correctly imports the method you need: #' Hello #' @export #' @importFrom rgeos coerce hello <- function() { a = matrix(1, 2,2) as(a, "gpc.poly") } This is a little…
-
1
votes1
answer67
viewsA: Export package
According to the book R-Packages Object Names Variable and Function Names should be lowercase. Use an underscore (_) to Separate words Within a name (reserve . for S3 methods). Camel case is a…
ranswered Daniel Falbel 12,504 -
4
votes1
answer279
viewsA: Pass data CSV file to array in R (BARPLOT)
I believe what you want is something like this: x <- data.frame(x = 1, y = 2, z = 3) names(x) <- c("a", "b", "c") barplot(height = as.numeric(x[1,]), names.arg = names(x)) Note that: >…
ranswered Daniel Falbel 12,504 -
7
votes2
answers78
viewsA: Functions with a number of "dynamic" arguments
dot-dot-dot or ... is called ellipse. minha_funcao_com_elipse <- function(...) { input_list <- list(...) input_list } See the result of this function: > minha_funcao_com_elipse(a = 1, b= 2,…
ranswered Daniel Falbel 12,504 -
3
votes1
answer135
viewsA: Web Scraping: How to change the value of a drop down button on a site using R?
This site has a very boring way of requesting POST, but he has the advantage of accepting requisitions GET also. To the GET, it uses a format…
-
4
votes1
answer131
viewsA: How to fit a Lowess curve into longitudinal data using R?
Your code has two problems: The loess function only accepts numeric variables, in case a semana is a factor. The geom_smooth always does the loess(y ~ x). With the group = id ggplot is trying to…
-
1
votes1
answer367
viewsA: Fit models for meta-regression calculation in R Studio
Whereas you spun it on R > madad(cbct.pr.sa) > summary(fit.cbct.pr.sa.Metodo) > summary(cbct.pr.sa$Metodo) > fit.cbct.pr.sa.Metodo <- reitsma(cbct.pr.sa, + formula = cbind(tsens,…
-
3
votes1
answer115
viewsA: Operation with very large size lists
I couldn’t streamline your code unless I proposed a solution that runs in parallel. teste2 <- function(lista1, lista2, progress = F){ lista1 = lapply(lista1, as, Class = "gpc.poly") lista2 =…
-
5
votes1
answer279
viewsA: 3D histogram using ggplot2
In your case I’d make a chart like this: library(ggplot2) library(tidyr) library(dplyr) teste %>% data.frame() %>% mutate(x = 1:10) %>% gather(y, z, -x) %>% mutate(y = y %>% gsub("X",…
-
7
votes3
answers1460
viewsA: Area of intersection of two polygons
You can get an approximation using Monte Carlo method: a = matrix(c(0 ,0 ,2 ,0 ,2 ,2 ,0 , 2, 0, 0), byrow = T, ncol = 2) b = matrix(c(.5, 0 ,1 , 1, 1.5, 0, .5, 0), ncol = 2, byrow = T) library(sp)…
ranswered Daniel Falbel 12,504 -
2
votes1
answer109
viewsA: Create package on Windows
On Rstudio, click on Project > New Project Then click on New Directory Then click on R Package Name the package. It cannot contain spaces nor - in addition to other rules. Rstudio will open a…
ranswered Daniel Falbel 12,504 -
7
votes1
answer3996
viewsA: Get the module of a negative number in R
Use the function abs (value absoluto): > v <- c(1,-2,3,-4) > abs(v) [1] 1 2 3 4
ranswered Daniel Falbel 12,504 -
7
votes1
answer473
viewsA: How to make the webscrapping of a site that has post method?
In that case you can do so using the package httr: library(httr) library(rvest) library(purrr) library(stringr) url <- "http://www.diariodemarilia.com.br/resultado/" res <- POST(url, body =…
-
1
votes1
answer219
viewsA: Stargazer changing position of dependent variable
Difficult to leave as complete as the table produced by stargazer, if you can simplify it, you can do so: modelos <- list(a,b,c,d) param <- plyr::ldply(modelos, function(x) coef(x)) param…
-
4
votes1
answer158
viewsA: How to extract every umpteenth element from a list?
You can use base p/ get the third element of each vector: > sapply(lista, function(x) x[3]) [1] "45" "49" "50" "57" "58" "55" The same thing you can do using the package purrr with the syntax a…
-
3
votes1
answer760
viewsA: How to sort boxplots by average or median on R
You need to turn the FID variable into factor orderly. It can be done like this: dados <- data.frame( FID = rep(1:5, each = 5), y = 25:1 ) medianas <- tapply(dados$y, dados$FID, median)…
ranswered Daniel Falbel 12,504 -
3
votes1
answer1572
viewsA: How to chart profiles in R?
Using the dplyr, tidyr and ggplot2 you can do so: library(dplyr) library(tidyr) library(ggplot2) dados %>% mutate(id = row_number()) %>% gather(idade, distancia, -id) %>% mutate(idade =…
ranswered Daniel Falbel 12,504 -
2
votes2
answers3321
viewsA: Convert Character Frame Data Column to Numeric
O R does not understand , as decimal separator, so you first need to replace them with dots and then turn into numeric. It can be done like this: s <- c("49,74", "15,98", "59,4") library(stringr)…
ranswered Daniel Falbel 12,504 -
7
votes1
answer83
viewsA: How to use contents of an array to mount the name of a file to be saved?
Just you replace the nomes[i]".txt" for paste0(nomes[i],".txt"). The loop will stay like this: for(i in 1:ncol(dados)){ write.table(dados[,i], paste0(nomes[i],".txt"), sep = "\t", row.names = F,…
-
3
votes1
answer73
viewsA: Nesting error
You can do it as follows, instead of adding layers to your chart. The parameter group in the ggplot makes it a graph p/ each group, without the need to add layers to the graph. teste =…
-
1
votes3
answers319
viewsA: Remove dataset elements by factors
You can do so too: > dataset %>% + group_by(fatores) %>% + summarise(n = n()) %>% + filter(n > 2) %>% + left_join(dataset, by = "fatores") %>% + select(-n) Source: local data…
ranswered Daniel Falbel 12,504 -
4
votes3
answers22504
viewsA: How to change the name of a column
Since variable names have carcter not recognized directly by R, you need to use the character: ` wrapped in their name. Then it would be necessary to do so: meus.dados <- data_frame( posicao =…
ranswered Daniel Falbel 12,504 -
1
votes2
answers87
viewsA: Somatoria accumulating columns in a Matrix in R
A way using the packages dplyr and tidyr would be the following: > library(tidyr) > library(dplyr) > > temp <- matrix(1:25, 5, 5) > temp <- data.frame(id = 1:5, temp) > temp…
-
5
votes1
answer203
viewsA: Graphical package faster than ggplot2
The very one base is much faster than the ggplot2 in terms of the time to render the chart. > library(ggplot2) > dados <- data.frame(x = 1:10, y = 1:10) > microbenchmark::microbenchmark(…
ranswered Daniel Falbel 12,504 -
12
votes2
answers7712
viewsA: Search for values in one data.frame and add to another (R)
Using dplyr you can do so: > library(dplyr) > Teste3 <- left_join(Teste2, Teste %>% select(ISIN, AVG_VOLUME)) Joining by: "ISIN" > Teste3 X1 X2 ISIN AVG_VOLUME 1 0.5896936 0.1847698…
-
2
votes1
answer78
viewsA: What is the alpha argument of the ltsReg function of the robustbase package?
Estimates should be equivalent when you use alpha = 1. See the example below: > library(robustbase) > data(heart) > coef(ltsReg(clength ~ height + weight, data = heart, alpha = 1))…
-
5
votes4
answers1339
viewsA: Equivalent to Excel SOMASES, in R
The way I like it the most is the following, using the packages dplyrand tidyr that are specialized for transformations in data.frames: library(dplyr) library(tidyr) df %>% group_by(Day.of.Week,…
ranswered Daniel Falbel 12,504 -
3
votes2
answers16023
viewsA: How to correlate qualitative and quantitative variables in R?
One way to obtain a coefficient that measures the intensity of the association between a categorical variable and a continuous variable is to use a square root of the coefficient of determination of…
ranswered Daniel Falbel 12,504 -
1
votes1
answer507
viewsA: Segmented Return in R
Consider the example of help(segmented) To adjust the segmented regression model, you first need to adjust the linear regression model. set.seed(12) xx<-1:100 zz<-runif(100)…
-
3
votes1
answer107
viewsA: bRasilLegis - XML content does not Seem to be XML
The problem is not in your command. Apparently this query is invalid and the camera server itself returns this result. >…
-
1
votes1
answer445
viewsA: How to create a dynamic progress bar in R?
It is not ideal, but you can try to do so by using the package progress. library(progress) total <- sum(sapply(lista, function(x) dim(x)[1])) pb <- progress_bar$new(total = total) for(t in…
-
4
votes2
answers465
viewsA: Is there a function that checks if a certain value is contained in a certain range?
You can get the same result by making a comparison less. As explained in response from the OS. the Trick is to substract the Middle between low and high from x, and then check whether that…
-
4
votes1
answer2373
viewsA: How can I get the current date and time in #R?
The function that does this is Sys.time(). It returns the date and time, along with Timezone. > Sys.time() [1] "2016-03-08 07:11:57 BRT" If you only want the date can use the function Sys.Date().…
-
2
votes2
answers1031
viewsA: How to extract a specific string snippet
You can do using the package stringr and regular expressions. In your case, I would do so: s <- "/ac/rio-branco/xpto-xyz-1-0-16-5-abcd-a1G57000003DE4QEAW" stringr::str_extract(s, "a1G\\S+\\s*")…