Posts by Rui Barradas • 15,422 points
432 posts
-
4
votes3
answers121
viewsA: How to create groups of 3 words in a text of random size?
I don’t know if it’s the most efficient code possible, but try the following. set.seed(530) # para ter código reprodutível frase2 <- lapply(strsplit(frase$texto, " "), function(x){…
ranswered Rui Barradas 15,422 -
1
votes3
answers5361
viewsA: Tell Distinguished in R
Your example of outcome seems to be wrong, there is two lines with Filial equal to JCT. Also, we cannot use class objects character, who have R$, so I just read the numbers, without the unit. agg…
ranswered Rui Barradas 15,422 -
3
votes2
answers80
viewsA: Indicator in variable-conditioned R with duplicate values
Now that it’s answered and accepted it may not matter but here it goes. Only with R base, we can use ave. df$Indicador <- as.integer(ave(df$IF, df$Município, FUN = function(x) all(tolower(x) ==…
-
6
votes1
answer1618
viewsA: How to create a column in R under specific conditions?
Just make a logical condition composed of the various elementary conditions of the question. data$novaCol <- with(data, as.integer(y == 2 & w == 4 & 15 < z & z < 25)) Like the…
ranswered Rui Barradas 15,422 -
7
votes1
answer151
viewsA: Customize boxplot
Does the following serve? First plot an empty graph, just to define the limits on the axes x and y. Then comes the real graph. Note the arguments axes = FALSE and add = TRUE at the end of boxplot.…
-
5
votes2
answers126
viewsA: Generate repeated values in R
Try the code below. I think that’s what you want. resultado <- lapply(seq_along(dados$Dia)[-nrow(dados)], function(i) { data.frame(Dia = seq(dados$Dia[i], dados$Dia[i + 1] - 1), Ganho =…
ranswered Rui Barradas 15,422 -
6
votes1
answer1440
viewsA: Arrange bars of a bar graph in R
The first thing to do is to get a vector of abbreviated days of the week. For this I will use the functions Sys.Dat and weekdays. Then I order and apply the inverse function (again order). # Hoje é…
-
4
votes1
answer1513
viewsA: Array Column Names using R
Use paste together with seq_len. rn <- paste("t", seq_len(n_forward), sep = "+") rownames(matriz) <- rn seq_len creates the sequence 1, 2, ..., n_forward. And paste does the rest. Note that it…
ranswered Rui Barradas 15,422 -
1
votes2
answers1913
viewsA: R - Search lines in a data frame conditioned on the part of a string
One way will be this. First let’s create an artificial table, just for testing. set.seed(2203) # torna os resultados reprodutíveis s <- "Tenho um data frame com várias linhas, nessas linhas tenho…
-
3
votes2
answers126
viewsA: Loop to calculate maximum value
If you want a function similar to cumsum, but calculating the accumulated maximum, here it goes, but I used a different name than yours to be consistent with the R groundwork. cummax <-…
ranswered Rui Barradas 15,422 -
7
votes1
answer376
viewsA: Extract table from a website for Rstudio
For that I usually use the package XML. Allows you to tell which table of the web page interests. In this case this page has several. The third has nothing of interest, so I extracted the numbers 1,…
-
1
votes2
answers71
viewsA: Calculate the product of an operation between two dataframes conditionally
You must first create the new column, then you can assign the respective values of the calculation. i1 <- df1$med == df2$med i2 <- df1$trat == df2$trat df1$ddap_cm <- NA df1$ddap_cm <-…
ranswered Rui Barradas 15,422 -
9
votes1
answer1169
viewsA: Function to validate a CPF in R
Well, since no one answered, although I agree with the above comments, today is Sunday, here it goes. First of all, you could have actually included some information about this CPF. Read Rafael…
ranswered Rui Barradas 15,422 -
2
votes1
answer70
viewsA: Store 10 values in R
Try the following: conta <- 0 x <- numeric(0) while(conta < 10){ y <- scan(nmax = 1) x <- c(x, y) conta <- conta + 1 } positivo <- x[x > 0] negativo <- x[x < 0] zero…
ranswered Rui Barradas 15,422 -
4
votes2
answers130
viewsA: Check if there are different elements in a list in R
The following instruction lapply creates a new column called DUPL_CLASSE in each data.frame. If all values in the column CLASSE are the same, the new column is all FALSE, otherwise it’s all TRUE.…
-
1
votes1
answer65
viewsA: Read a typed number (R)
The answer is very simple, just use scan without any argument. print("Digite um numero") numero <- scan() nAntecessor <- numero - 1 cat("O antecessor é: ", nAntecessor, "\n") Note that I…
ranswered Rui Barradas 15,422 -
3
votes2
answers416
viewsA: Sort column of a data frame in R
How do we not have an example of base.dados, I created a data.frame. If you want to avoid so many ifelse can do something like this. set.seed(6399) # Torma o código reprodutível classe1 <-…
ranswered Rui Barradas 15,422 -
3
votes2
answers109
viewsA: How to add a column
Thank you for the data format dput. After viewing the data, the answer is very simple. The correct way to calculate the z-Scores with scale is the following. train.scale <-…
ranswered Rui Barradas 15,422 -
5
votes2
answers538
viewsA: How to select data in R?
In fact, it’s not a good idea to do this by hand, let alone have so many objects in the globalenv. The best is to create these sub data.frames in a list with, for example, split. set.seed(4577) #…
ranswered Rui Barradas 15,422 -
3
votes1
answer199
viewsA: How to compare 3 different variances?
I’m going to assume that the samples are from normal variables, with mean 100 and standard deviation 12. To test the homogeneity of variances, there is the Levene test, which can be found in the…
ranswered Rui Barradas 15,422 -
3
votes1
answer58
viewsA: Degree of class grouping
A possible solution is to use a double cycle with lapply to create a list of common students and their proportions. For this, it is better to have the classes together in lists. lista_t1 <-…
ranswered Rui Barradas 15,422 -
3
votes2
answers94
viewsA: Transform duplicate values to 0 by keeping an element in the conditional R
See the function duplicated. dados$z[duplicated(dados$z)] <- 0 dados x y z 1 a 2015 122.4 2 a 2015 0.0 3 b 2016 200.5 4 a 2014 300.6 5 c 2016 80.1 If you want to keep the original database, make…
ranswered Rui Barradas 15,422 -
1
votes1
answer44
viewsA: Reverse transformation in R to var continues with max = Inf
You can try adding a very small value to zero values. For example inx <- which(banco1$rac == 0) banco1$rac[inx] <- banco1$rac[inx] + .Machine$double.eps Note that the inverse of…
-
4
votes2
answers101
viewsA: Complete observations in a data frame
Only with R base, you can use expand.gridfollowed by merge. tmp <- expand.grid(Ano = unique(dados$Ano), Categoria = unique(dados$Categoria)) res <- merge(dados, tmp, all.y = TRUE)…
ranswered Rui Barradas 15,422 -
1
votes3
answers94
viewsA: Find exponent of the data-fit equation, R
Either I’m completely wrong or lm enough to determine p. y ~ x -p <=> y ~ e -plog(x) <=> log(y) ~ -plog(x) Then we adjust this last linear model. fit <- lm(log(y) ~ 0 + log(x))…
ranswered Rui Barradas 15,422 -
3
votes1
answer41
viewsA: create sequence that increases and decreases monotonically
First the data. a <- scan(text = "64.42 66.99 100.39 97.96 97.96 96.26 94.22 92.35 86.05 84.01") Now, if my comment above is correct, we can solve the problem with a single line of code R. d…
ranswered Rui Barradas 15,422 -
3
votes2
answers97
viewsA: Chaining - R
With Rafael’s data, it will be simpler to use the function expand.grid. LOJA = 1:3 Produtos = c("camera", "Smartphone", "TV") res <- expand.grid(LOJA = LOJA, Produtos = Produtos)[, 2:1] res…
ranswered Rui Barradas 15,422 -
1
votes1
answer1068
viewsA: R code for likelihood testing
Now it’s easier to answer the question. Just see the section Value page ?glm. L_sat <- GLM.1$deviance/(-2) L_red <- GLM.1$null.deviance/(-2) Notes further that it is possible to simplify the…
ranswered Rui Barradas 15,422 -
3
votes2
answers69
viewsA: How to create function where I need to pass the parameter inside a text block in R
See ? For this, argument Collapse. Within the function this should solve the problem. Metodo <- paste('<?xml version="1.0" encoding="utf-8" ?>…
-
1
votes2
answers63
viewsA: Not Available (how to put information in this position)
The best way to give an example of data, in this case a data.frame is to use the command dput: dput(dat) # postar o output disto Let’s first produce a data.frame example. set.seed(2174) dat <-…
ranswered Rui Barradas 15,422 -
2
votes1
answer1223
viewsA: Function tapply - arguments must have the same length
When you make that mistake, most of the time it’s because you have to use ?ave and not tapply. dados$valor_medio <- round(dados$Valor_Total/dados$Qtde,2) dados$media <- ave(dados$valor_medio,…
ranswered Rui Barradas 15,422 -
7
votes3
answers243
viewsA: How to filter data according to part of the characters of a variable?
Using Marcus' answer, I just want to draw attention to something that often goes unnoticed by "new" users of R, which is the variable given$Name being of class factor. This has importance in the…
ranswered Rui Barradas 15,422