Posts by Rcoster • 1,779 points
38 posts
-
2
votes1
answer84
viewsQ: Encoding problem in a file folder
I switched computer today on the service and my codes stopped working, giving encoding error in the files folder: > setwd("I:/AGG/01. DIGEP/Relatórios Gerenciais/Edição/2018-08-17") > getwd()…
-
2
votes3
answers3012
viewsA: Aggregate function on R
A solution using aggregate() is to inform . in the left part of the formula: dados <- structure(list(REFERENCIA = c("JAN_2017", "JAN_2017", "JAN_2017", "JAN_2017", "FEV_2017", "FEV_2017",…
-
0
votes1
answer340
viewsA: Matlab to R translation with system(sprintf())
Try: rm(list = ls()) nsta <- c(1, 2, 3) npx <- c(2, 3, 4, 5) npu <- c(2, 3, 4, 5) nmax <- 2500 nome <- "MODEL1b" file.remove(sprintf('%s.log', nome)) nfiles <- 0 for(k in…
-
3
votes1
answer175
viewsQ: Invert factors in only 1 bar in ggplot2
Does anyone have any idea how the order of the factors in only 1 bar in ggplot2? Reordering the data no longer works :( In case I would like to reverse the first bar, so that the green was up and…
-
3
votes1
answer73
viewsA: Timeout when using httr:POST()
The httr package, for some reason, does not use the computer’s proxy configuration. To resolve, you need to load the package devtools and call the following function:…
-
5
votes1
answer73
viewsQ: Timeout when using httr:POST()
Expensive, I’m trying to hold a conference of records at CREA-RS website, but every time this giving the following error: Error in curl::curl_fetch_memory(url, handle = handle) : Timeout was reached…
-
4
votes1
answer117
viewsA: How to export a polygon created by drawPoly as shapefile?
The package rgdal allows you to do this through the command writeOGR(). Assuming your cell is in the variable poly, the following command would be sufficient: writeOGR(obj = poly, dsn =…
-
4
votes1
answer1174
viewsA: How to find the position of equal elements in character vectors (Character)?
If you seek elements in a who are exactly equal to the elements in b, the best option is to use %in%: a<-c("Ano", "X2751006.", "X2751007.", "X2751008.", "X2751015.", "X2751017.", "X2751018.",…
-
18
votes7
answers1690
viewsA: Hexadecimal for RGB
It is more a question of computational mathematics/mathematics than programming. Follow my contribution: The RGB code, as the name says, consists of 3 elements: Red, Green and Blue. In this case,…
-
3
votes2
answers632
viewsA: How to group microdata of census persons by residence?
Your mistake is in that part: censo_cf18 <- censo_cf[which(censo_cf$V6660 < 18),] The moment you do that, you’re cutting off 1) men (this variable only exists for women) and children.…
-
1
votes2
answers175
viewsA: Apply function in data groups
The problem is that a vector is being passed to the function homo() (dados$valor) and within it you’re treating it as a date.frame/list (trying to call a$valor, among others.) Below a function…
-
5
votes2
answers344
viewsA: How to make a matrix in R that its inputs are equal to i * j?
An alternative to applying a function in all combinations of 2 vectors is the function outer(). In the case: > outer(1:10, 1:10, '*') [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 2 3…
-
2
votes2
answers1112
viewsA: Reading Archives in ASCII Census 2010
These files are not made available by IBGE. What IBGE makes available is a file with layout of each bank (Layout / Layout_microdados_sample.xls), which allows you to make your own syntax.…
-
6
votes2
answers13721
viewsA: Change the X-axis scale
If you are using basic R commands to chart (plot()), you can change the x/y axis with the parameter xlim/ylim x <- rnorm(100, 10, 2) y <- rnorm(100, 100, 20) plot(x, y) plot(x, y, xlim = c(0,…
-
2
votes2
answers379
viewsA: Repeating structure of a function
To achieve the expected result, the for() should be within the function - and not outside: rf <- function(n, a, b, v) { u <- runif(n, 0, 1) out <- matrix(0, n, length(v)) for (k in…
-
6
votes3
answers7652
viewsA: Graphic overlay on R
If your goal is the same as in the example, one solution is to use the matplot() a = rnorm(1000) b = runif(1000) matplot(cbind(a, b), type = 'l') It is also possible to do with the ggplot:…
-
2
votes2
answers1265
viewsA: Daily Time Series Decomposition
Bruno, I believe the error is in the command used, because what you passed us uses a variable to create the series (QHE.ts) and another for decomposition (QUHE.ts). Try: QHE.ts <- ts(QUHE.z, freq…
-
2
votes2
answers871
viewsA: How to decode many columns of a R data frame
A solution using the base package: dados <- data.frame(replicate(10, sample(1:3, 10, rep = T))) dados_vars <- data.frame(variable.name = paste0('X', 1:10), data.type = sample(1:4, 10, rep =…
-
4
votes1
answer1219
viewsA: Filter and save to csv
To save in the formed CSV to use the write.csv2() (and to read, read.csv2()). Besides, you can use the subset() in a data.frame, greatly simplify your code: dados <-…
-
1
votes1
answer242
viewsA: R filter sets
A solution to this case would be to use the function aggregate. dados <- read.table(text='Meses Pessoa Abril Joao Março Ana Abril Carlos Junho Joana Março Pedro', header = T) dados_agregados…
-
3
votes2
answers106
viewsA: Data window moving in time (t)
A possible solution without using the window, using indexers to pick up only a part of the time series (the [], if I have another name, let me know!) prev <- se <- NULL for (i in 0:12) {…
-
1
votes2
answers513
viewsA: Survey and count package
It is not necessary to use Survey, mainly for simpler procedures. In this case, a simple aggregate() would be enough for you. I don’t have the PNAD data on my computer right now, but follow an…
-
1
votes2
answers296
viewsA: What does the "Design has only one Primary sampling Unit" error mean?
Without code it is difficult to help you, but this error is usually associated with the variance calculation (which obviously needs more than one observation). It is possible to circumvent this by…
-
1
votes2
answers139
viewsA: How to use the svymean() function if I have missings in the observations?
Daniel’s answer is correct: just add the parameter na.rm = TRUE. I just add that care should be taken when using this parameter when we analyze more than 1 variable at the same time (I do not know…
-
1
votes2
answers3637
viewsA: How to fix the x-axis boundaries in ggplot?
Andrei, the parameter limits serves to set the limits and not the points. For what you want to use the parameter breaks ggplot(dados, aes(x = ano, y = k)) + geom_point() + geom_line() +…
-
2
votes2
answers886
viewsA: How to perform a cross-tabulation and weighted?
It is possible to obtain through the xtabs: dados <- data.frame(minusculas = sample(letters[1:5], 100, rep = TRUE), MAIUSCULAS = sample(LETTERS[1:5], 100, rep = TRUE), peso <- rchisq(100, 10),…
-
3
votes2
answers132
viewsA: How to create a pop-up warning that the script has come to an end?
Another solution is to use the tcltk package. This package is already installed in R, although it does not appear in the CRAN package list. require(tcltk) tkmessageBox(title = 'Exemplo tcltk',…
-
3
votes4
answers5405
viewsA: Strategies to analyze very large databases in R (that do not fit in RAM)
There is always the option to work with external databases on R and load only the variables needed for the analysis (since it is rare that the analyses will use all variables at the same time).…
-
4
votes1
answer101
viewsA: Convert Character with precision
The problem is in == and not in class. The == works only for one value. To compare with more than one value, it has to be %in%. In your case: desp2[desp2$codigo_novo %in% c('1049', '1001'),]…
-
3
votes1
answer361
viewsA: Understanding the Survey
The package survey is used for the analysis of complex samples. That is, where not all elements have the same probability of being sampled, and that is where the parameters come in strata. weight…
-
1
votes2
answers446
viewsA: Function using as arguments successive values of a column (R)
The solution using the rollapplywould be: require(zoo) set.seed(1) dados <- data.frame(x=rnorm(100), y=rnorm(100)) rollapply(dados, FUN = function(x) (x[1]/x[2])^12 - 1, width = 2, align = 'r')…
-
4
votes3
answers304
viewsA: How to create a Standard Deviation Mobile Window (R)
Da to use the command rollapply of the zoo package along with the apply base: dados <- matrix(rnorm(100), ncol = 10) require(zoo) apply(dados, 2, function(x) rollapply(x, width = 5, FUN = sd,…
-
12
votes5
answers12797
viewsA: How to consolidate (aggregate or group) the values in a database?
Another option is to use the package data.table: df <- data.table(df) df[,sum(vendas), by=vendedor] df[,sum(vendas), by=data.table(vendedor, regiao)] Packages like the plyr and the data.table…
-
7
votes3
answers3287
viewsA: How to put the regression equation on a graph?
Follows a possible solution, using geom_text: set.seed(1) x <- rnorm(100) y <- rnorm(100) + 2*x +10 modelo <- lm(y~x) coeficientes <- modelo$coefficients texto <- sprintf('y = %.2f +…
-
9
votes4
answers8280
viewsA: How to put the regression line on a graph?
Another alternative is to use the package ggplot2: set.seed(1) x <- rnorm(100) y <- rnorm(100) + 2*x +10 require(ggplot2) dados <- data.frame(x=x, y=y) # O ggplot2 exige que os dados…
-
3
votes3
answers180
viewsA: How to include a variable high to n in regression
Whenever you want to use a function of some variable, you can use the function I(). x<-rnorm(100,1,100) y<-rnorm(100,0,10)+2*x+x^2 mod <- lm(y~x+I(x^2)) The advantage of using I() in…
-
7
votes3
answers17265
viewsA: How to sort a column-by-column data.frame in R?
You can do it right on order, simply enter multiple parameters. set.seed(1) dados <- data.frame(w=rep(c("A", "B"), 2), x= rep(c("D", "C"), 2), y= rnorm(4), z=rnorm(4), stringsAsFactors=FALSE)…
-
5
votes2
answers5010
viewsA: How to correctly position the Abels in the barplot?
A solution with the ggplot2: dados <- read.table(text='8 5 4 6 4 4 2 41 58 15 19 19 33 30 60 59 67 54 49 59 56 43 30 66 73 80 56 64', header=F, sep=' ') fator_tecnologico_labels <- c(…