Posts by Rafael Cunha • 4,954 points
101 posts
-
8
votes1
answer57
viewsA: add column that shows how many times a value has repeated from that row back
Only use the function seq library(dplyr) dataset %>% group_by(NOME) %>% mutate(COLUNA_QUE_QUERO_CRIAR = seq(1:n()))
ranswered Rafael Cunha 4,954 -
4
votes3
answers121
viewsA: How to create groups of 3 words in a text of random size?
Follows code that returns the expected result: frase <- data.frame(id = c(1,2), texto = c("palavra1 palavra2 palavra3 palavra4 palavra5", "palavra6 palavra7 palavra8 palavra9 palavra10…
ranswered Rafael Cunha 4,954 -
1
votes3
answers5361
viewsA: Tell Distinguished in R
Using the package dplyr library(dplyr) dados %>% group_by(Filial) %>% summarise(Contagem = length(unique(Matrícula)), Valor = sum(Valor))
ranswered Rafael Cunha 4,954 -
1
votes1
answer283
viewsA: Data preparation in R for cluster use
As Rui said in the comment, the image of your data does not help us to help you. As for doubt, the function scale needs your database to be a numerical matrix. One solution is for you to transform…
ranswered Rafael Cunha 4,954 -
2
votes2
answers80
viewsA: Indicator in variable-conditioned R with duplicate values
I don’t know if there is a more direct way, but the code below solves your problem. library(dplyr) df %>% group_by(Município) %>% mutate(Indicador = ifelse(IF == "Cooperativa", 1, 2)) %>%…
-
3
votes2
answers195
viewsA: Create a calendar dimension with the month before Sys.date.
I added the function slice in your code list(mes = 1:12, ano = 2015:2017) %>% purrr::cross_df() %>% dplyr::mutate(data = as.Date(sprintf("%d-%02d-01", ano, mes))) %>% dplyr::filter(data…
-
3
votes1
answer400
viewsA: Create boxplot graph of values in classes
dfTempoNota <- structure(list(NotaFinal = c(23.95, 25.4, 31.55, 25.4, 27.8, 27.3, 31.85, 20.45, 31.95, 28.55, 20, 24.95, 14.45, 22.55, 25.65, 10.35, 27.95, 21.45, 18.45, 21.1, 12.3, 22.65, 30.35,…
ranswered Rafael Cunha 4,954 -
4
votes7
answers9711
viewsA: Remove accents
Function stri_trans_general package stringi stri_trans_general("Arapeí", "Latin-ASCII")
ranswered Rafael Cunha 4,954 -
1
votes1
answer310
viewsA: Calculate average and deviation time series data
See if that’s what you need: library(dplyr) DfConsumo %>% group_by(Animal) %>% summarise(Consumo = first(Consumo)) %>% summarise(Média = mean(Consumo), Desvio = sd(Consumo)) DfConsumo…
-
6
votes1
answer2388
viewsA: Merge data frames
The function merge with the option all = T solves your problem. See code below: DfConsumo <- data.frame( Animal = c(rep(5,9), rep(6,9)), Dia = c(2,4,5,6,7,8,9,10,11,3,4,5,6,7,8,9,10,11), Consumo…
ranswered Rafael Cunha 4,954 -
4
votes2
answers126
viewsA: Generate repeated values in R
The package zoo contains the function na.locf that does exactly what you need. According to its description, it is a generic function that replaces each NA with the latest non-NA previous. Follows…
ranswered Rafael Cunha 4,954 -
4
votes2
answers186
viewsA: Run command for each column of a date.frame
In your code, you create the object names_dwc and does not use it within the for. In the case names_dwc[k] <- paste('dwc_ST', k, sep = "") creates only one vector of characterand does not create…
-
3
votes1
answer885
viewsA: Group columns in the R bar graph
Let’s go in pieces, a) To group the bars, include the space array space = c(0.5, 0, .5, 0, 0, 0) in the building code of your bar chart. This option specifies the space before each bar, and you can…
-
5
votes2
answers830
viewsA: Index searches on an R vector
You can create two indices that identify even and odd positions x <- rnorn(1500) par <- (1:length(x) %% 2) == 0 impar <- (1:length(x) %% 2) != 0 then just call the vector with these indexes…
-
1
votes2
answers1913
viewsA: R - Search lines in a data frame conditioned on the part of a string
Provided that your data.frame be it dados and the column containing the word "Ad" is frase, dados[grep("Anúncio", dados$frase),] should solve your problem…
-
3
votes1
answer119
viewsA: Saving rows of a data frame based on the values of a column of another data frame
created two dataframe to exemplify A <- data.frame(X = sample(LETTERS, 50, rep = T), Y = rnorm(50), Z = rpois(50, 3)) B <- data.frame(X1 = sample(LETTERS, 10, rep = T), Y1 = rnorm(10)) for you…
ranswered Rafael Cunha 4,954 -
1
votes3
answers1220
viewsA: Use of the sub function in R - string with special characters
you put the backslash dados$Col_Nova <- sub(pattern = "[A-z].{2}cnic[A-z]\\s.*", "Técnico de enfermagem", dados$Col_Nova)
-
9
votes1
answer139
viewsA: R is not recognizing csv file tabs
In function read.csv separator options exist. In the case of the file with , specify within the function dados <- read.csv("arquivo.csv", sep = ",")…
-
1
votes2
answers1516
viewsA: How to create column from data contained in other columns
I only took columns that have at least one value 1 of your example and also simplified the name of variables to use in my code. base <- data.frame('Presc' = c(rep(0,7),1,rep(0,3)), 'Per_Dan' =…
-
4
votes2
answers957
viewsA: Lodown facility
I just did the test and the installation happened without errors. library(devtools) install_github("ajdamico/lodown" , dependencies = TRUE) The package archive, was installed with the aid of Rtools.…
ranswered Rafael Cunha 4,954 -
10
votes1
answer398
viewsA: Find the last day of the month
With the following function, you get the last days of the months following a given last day of month seq_monthly <- function(from,length.out) { return(from %m+% months(c(0:(length.out-1)))) } It…
-
3
votes2
answers130
viewsA: Check if there are different elements in a list in R
It may not be the most elegant shape, df <- data.frame(LISTA = names(dados), CLASSE = T) for(i in 1:nrow(df)){ ifelse(length(unique(dados[[i]]$CLASSE)) == 1, df$CLASSE[i] <- F, df$CLASSE[i]…
-
2
votes1
answer98
viewsA: Access a data frame object, within a function in R - Census 2010
You can save the data frames in a list. Your function would be: funcao_carga=function(diretorio,vetor_largura_fixa,vetor_variaveis) { setwd(diretorio) lista <- list() path <- diretorio files…
ranswered Rafael Cunha 4,954 -
3
votes1
answer127
viewsA: How to get the table with the data that generate the graph with the DECOMPOSE command in R
Following the help of function decompose require(graphics) m <- decompose(co2) plot(m) if you use names(m) 6 "objects" are saved inside m: x, seasonal, trend, random, figure and type. Now just…
ranswered Rafael Cunha 4,954 -
5
votes1
answer220
viewsA: Barplot arguments
To rotate you will need to use the command text to create the X axis. In function text there is the argument srt to specify the rotation degrees. Also include the argument xaxt = "n" in charge of…
-
4
votes2
answers94
viewsA: Transform duplicate values to 0 by keeping an element in the conditional R
Follow code, using the package dplyr. In that case, I take into account the grouping of x and y library(dplyr) dados <- structure(list( x = structure(c(1L, 1L, 2L, 1L, 3L), .Label = c("a", "b",…
ranswered Rafael Cunha 4,954 -
3
votes1
answer423
viewsA: How to change map scale in ggplot2?
I saw your post in the English version and got some progress. There is the function dms package GEOmap which turns decimal into degrees. Follow my attempt code. library(ggplot2) library(ggmap)…
-
2
votes2
answers381
viewsA: combine vectors per line in R by filling voids
There is the function smartbind package gtools who does what you need library(gtools) smartbind(c(1,2,3,4), c(1,2), fill = 0)
ranswered Rafael Cunha 4,954 -
5
votes2
answers938
viewsA: How to take the Hosmer-Lemeshow test on R?
With a quick search of the Stack Overflow in English and with the command RSiteSearch of own R found 2 functions for such test. hoslem.test package ResourceSelection and logitgof package…
ranswered Rafael Cunha 4,954 -
2
votes2
answers97
viewsA: Chaining - R
If your information is similar to what is in the expected result, LOJA = 1:3 Produtos = c("camera", "Smartphone", "TV") one merge resolves. dados <- merge(LOJA, Produtos) library(dplyr) dados…
ranswered Rafael Cunha 4,954 -
2
votes1
answer419
viewsA: Beta and OR confidence interval
For the confidence interval, there is the function confint confint(GLM.1) Follows a link which gives a detailed account of Logistic Regression in the R that can assist you with calculating the odds…
ranswered Rafael Cunha 4,954 -
3
votes1
answer1312
viewsA: Graphic text position using ggplot2
I found a suggestion in the Stack Overflow in English (in this topical). Adapting to your example, would look like this geom_text(aes(label=n, y = n+0.01), size = 3.0, position =…
-
5
votes2
answers326
viewsQ: Regular Expression R
I have a variable that indicates the year of Elementary School that a student is attending, but this variable was not fulfilled in a single way. For example the 1st Year of Elementary School because…
-
5
votes1
answer143
viewsA: Building new variables using dplyr
I managed to resolve, was missing include a . within the function select CLIENTES %>% mutate(CLIENTES.Dep = rowSums(select(., contains("Dep."))), CLIENTES.Usu = rowSums(select(.,…
-
7
votes1
answer143
viewsQ: Building new variables using dplyr
I have the following database Clientes.Dep..Gratuito.PCG Clientes.Dep..Gratuito Clientes.Dep..Não.Gratuito 0 0 0 0 0 0 25 0 0 0 0 2 0 0 79 0 0 71 Clientes.Usu..Gratuito.PCG Clientes.Usu..Gratuito…
-
8
votes2
answers961
viewsA: How to convert string (in English) to date?
I created the following database to use as an example dados <- data.frame( mes = c("junho 2017", "maio 2017", "junho 2017", "maio 2017", "junho 2017", "maio 2017", "junho 2017", "maio 2017",…
-
5
votes1
answer85
viewsQ: Code improvement
Good afternoon. I have the following data structure: structure(list(CIDADE = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label =…
-
1
votes1
answer261
viewsA: How to use the tablaStack function in R
It would be nice if you put the code you are using and the error message that appears. See if the code below answers your question library(epiDisplay) dados <- data.frame( Num = 1:3, DIASEMANA =…
ranswered Rafael Cunha 4,954 -
1
votes2
answers11355
viewsA: Select column of a data.frame --- Database Division in R
It would be easier if you included your database (or some part of it) so that we could work on it. Take a look at the function dput to that end. It would also be nice if you included the code you…
ranswered Rafael Cunha 4,954 -
3
votes1
answer511
viewsA: Calculation with Matrices in R
Where M is its matrix, Transposed t(M) Inverse solve(M) Identity diag(x = 1, nrow, ncol) Determinant det(M)…
ranswered Rafael Cunha 4,954 -
2
votes3
answers1016
viewsA: Split a data frame and save to different directories
tab <- data.frame("Nome" = sample(LETTERS, 100, rep = T), "Quantidade" = c(rep(1,20),rep(2,25),rep(3,30),rep(4,25))) tab1 <- tab[which(tab$Quantidade == 1),] tab2 <-…
ranswered Rafael Cunha 4,954 -
7
votes1
answer1654
viewsA: Text position on pie chart in ggplot2
I included the function position_stack() inside geom_text() graf.faixa.etaria <- ggplot(tabela.faixa.etaria, aes(x ="", y=Freq, fill=Var1)) + geom_bar(width = 1, stat = "identity") +…
-
2
votes1
answer597
viewsA: Replace matrix value (R)
M <- matrix(rpois(25, lambda = 4), ncol = 5) M [,1] [,2] [,3] [,4] [,5] [1,] 4 3 4 3 6 [2,] 3 2 6 1 6 [3,] 3 4 6 4 1 [4,] 9 3 3 4 4 [5,] 3 5 6 3 8 valor <- diag(M) valor [1] 4 2 6 4 8 M <-…
-
2
votes3
answers749
viewsA: Add currency rating in R
It may not be the most elegant shape (or with the help of some package), but you can do it in the arm First I used two functions (right and left) along the lines of functions direita and esquerda…
ranswered Rafael Cunha 4,954 -
1
votes1
answer695
viewsA: How to count different lines in R
Follow code for resolution tab <- data.frame( Material = c(rep(1,5), rep(2,3), rep(3,6), 4), Fornecedor = c("A","B","A","C","C","B","D","E","A","B","C","F","G","A","A") ) library(dplyr) tab…
ranswered Rafael Cunha 4,954 -
9
votes1
answer16303
viewsA: How to make and calculate the Frequency Distribution Table in R?
For the first part of the question, the code below can help you library(dplyr) dados <- c(18,19,19,19,19,19,19,19,19,19,19,19,20,20,22,23,24,26,26,30,32) tabela <-…
-
4
votes1
answer89
viewsA: Select data from df according to data from a list
follow code that returns the result you expect match <- x[which(x$nr %in% y), ]
-
2
votes1
answer284
viewsA: Select ID vectors with certain characteristics in R
First I created a function is.0() along the lines of is.na() to test whether the value of the cell is 0 is.0 <- function(x){x == 0} Then I used the package functions dplyr df <- df %>%…
-
3
votes1
answer125
viewsA: How to create a factor variable from other factors?
After creating the X variable, use the command as.factor() in that variable. df <- data.frame( Renda = c("baixa","inter","alta","alta"), Escolaridade = c("fund. incomp","superior","pos…
-
1
votes1
answer72
viewsA: Sentences in a Loop on R
See if this alternative suits you for(i in 1:2){ beta <- summary(lm(a ~ b,data))$coef[2,c(1,4)] coef.matrix[i,1]=beta[i] }
ranswered Rafael Cunha 4,954