Posts by neves • 5,644 points
165 posts
-
1
votes4
answers101
viewsA: Dealing with dates of heterogeneous formats in R
An even simpler solution is to use the function parse_date_time package lubridate. library(lubridate) y <- parse_date_time(x = x, orders = c("y m d H M S", "d m y H M")) y #[1] "2019-10-08…
-
1
votes3
answers82
viewsA: Merge two dataframes of the same name keeping all columns
You can do it like this: library(magrittr) merge(df1, df2, by = "row.names") %>% # agrega os dados .[-1] %>% # remove a coluna Row.names .[sort(colnames(.))] # ordena as colunas # Nom1.x…
-
0
votes5
answers8758
viewsA: How to remove line that has Missing?
You can also use the function drop_na of tidyr: Dice: df_1 <- data.frame( x = c(NA, 1:4), y = c(1:4, NA) ) Codes: library(tidyr) drop_na(df_1) # para remover NA de todo o banco de dados # x y #1…
-
1
votes3
answers5016
viewsA: Replace NA in R language
The function case_when package dplyr also does what you want. Before, note that there is a difference between NA (missings) and "" (Empty Cells), as quoted in this reply. First, reproduce your data:…
-
2
votes1
answer51
viewsA: How to round up a data.frame in R using the %> % dplyr operator?
With dplyr and stringr: library(dplyr) library(stringr) a %>% mutate(.data = ., across(.cols = IQA, .fns = ~ str_replace_all(string = ., pattern = ",", replacement = "."))) %>% mutate(.data =…
-
-2
votes3
answers34320
viewsA: How to import excel data pro R?
Well, about the excerpt: "As the code I’m making will be sent to someone else, I can’t put an absolute path, and I’m having trouble applying the function to a relative path." We can know what to put…
-
0
votes3
answers22504
viewsA: How to change the name of a column
We can use the function rename_with of dplyr, which makes use of functions (like those of the package stringr) to search for patterns and perform name substitution. Dice: df_1 <- data.frame(…
-
4
votes1
answer27
viewsA: Change/ abbreviate words from a data set
A simple way to create abbreviations is by using the function abbreviate base package. Since words can have accents, the function is used together iconv (also from the base package) to solve this…
-
3
votes1
answer45
viewsA: Transform a list (list) into a database (data.frame)
Firstly, it is necessary to transform the null elements (NULL) of that list. df_1 <- lapply(dados.lista, Filter, f = Negate(is.null)) Once done, aggregate the variables with rbind.fill. Thus:…
-
1
votes1
answer34
viewsA: How to separate data that is in the same column in Rstudio?
Supposing that this separator between date and time is the space, you can do so: Reproducible example: df_1 <- data.frame(value = c("10", "20"), Date = c("2021-10-01 03:15", "2021-10-01 00:20"))…
-
2
votes2
answers39
viewsA: Create a table in R from a date frame grouping the values per month
With dplyr and lubridate you can do so: library(dplyr) library(lubridate) df %>% mutate(.data = ., across(.cols = data, .fns = ~ ymd(.))) %>% mutate(.data = ., across(.cols = data, .fns = ~…
-
1
votes2
answers40
viewsA: I want to group 3 columns into one, separated by a comma in the R
Use unite: library(tidyr) unite(data = dados, col = x, c(x, y, z), sep = ", ") # x # A 1.1, 1.2, 1.3 # B 2.1, 2.2, 2.3
-
5
votes3
answers49
viewsA: Normality and fragmentation of the sample
You can use tapply: set.seed(1) df_1 <- data.frame( x = rnorm(n = 30, mean = 10, sd = 1), y = sample(x = 1:3, size = 30, replace = TRUE) ) tapply(X = df_1$x, INDEX = df_1$y, FUN = shapiro.test)…
-
0
votes2
answers67
viewsA: How to group by text [R]
Solution tidyverse: library(tidyverse) library(magrittr) df %>% mutate(.data = ., coluna2 = case_when( equals(e1 = ., e2 = 'lapis vermelho grande') ~ "caixa grande", equals(e1 = ., e2 = 'lapis…
-
4
votes2
answers57
viewsA: Is there a "dd-mmm-yyyy" format (e.g., "13-feb-1980") in the R?
With tidyverse: library(tidyverse) library(lubridate) df %>% mutate(across(.cols = dt, .fns = ~ str_to_lower(month(., abbr = T, label = T)), .names = "mes")) %>% separate(col = dt, into =…
-
2
votes2
answers49
viewsA: Convert days to years using R lubridate
With difftime: library(dplyr) library(lubridate) In weeks weeks: data.frame(inicio= ymd(19800101), fim=ymd(20200101)) %>% mutate(dif = difftime(fim, inicio, units = "weeks")) In years (division…
-
0
votes4
answers4556
viewsA: How to change the order of appearance of Columns in a Data Frame?
One solution is to use the function relocate, of dplyr, built for this purpose: library(dplyr) df %>% relocate(Indice) Your advantage over dplyr::select is not needing to write all variables. The…
-
2
votes1
answer90
viewsQ: What does "foo() <-" mean in R?
Consider the vector k class factor three-tiered (1, 2 and 3): k <- as.factor(x = sample(x = 1:3, size = 30, replace = TRUE)) [1] 3 1 1 2 1 1 3 2 3 3 1 2 3 3 2 2 2 1 3 1 2 2 1 3 1 1 3 2 1 1…
-
0
votes1
answer50
viewsA: What does the "~" operator mean in tidyverse context?
The operator ~ in the context tidyverse is a proxy of the anonymous function. Instead of writing: function(k) {is_weakly_greater_than(e1 = k, e2 = 40)} I can replace function(k) {...} for ~. Thus: ~…
-
3
votes1
answer50
viewsQ: What does the "~" operator mean in tidyverse context?
Suppose the data set below: df_1 <- structure(list(var_1 = c(42.0324095338583, 86.828490421176, 42.4499513395131, 87.8373390808702, 69.4962524808943), var_2 = c(52.6775231584907,…
-
2
votes2
answers129
viewsA: Loop Operation with Multiple Data Frames
No need to apply loops to do as you wish. With tidyverse you can do this. library(tidyverse) First: Separate the data into different data frames using the "code" column, that is, in this example, I…
-
2
votes1
answer71
viewsQ: How to order by class and by descending order?
Suppose the following database: set.seed(1) df_1 <- data.frame(x = replicate(n = 3, expr = sample(x = 1:3, size = 10, replace = TRUE))) By the way, with the package dplyr I tried to:…
-
2
votes2
answers673
viewsA: How to sum a column in R
Just insert the function summarize: library(tidyverse) dados %>% filter(data == max(dados$data)) %>% select(-grep("obitosAcumulados", colnames(dados))) %>% summarise(var =…
-
3
votes2
answers1104
viewsA: How to eliminate variables that have some NA values in R
Consider the following data set: dados <- data.frame(x = c(1, 2, 3, 4, 5, 8), y = c(NA, 0, 0, 0, 2, NA), w = c(88, 2, 3, 4, 5, 8), z = c(5, 2, 2, 9, NA, 18)) To eliminate the varnishes containing…
-
2
votes1
answer54
viewsQ: Locking the limit of one widget (Shiny) as a function of another
I tried to stop the limit of one widget based on the change of another. Suppose these two cases: First case: The sum of two buttons cannot exceed the value 100. For example, when I put the value 60…
-
1
votes1
answer47
viewsA: How can I add a filter to list only the countries above x cases?
Just add one filter at the end of the function: library(dplyr) covid <- confirmed %>% select(Country.Region, X3.29.20) %>% group_by(Country.Region) %>% summarise(sum_X3.29.20 =…
-
1
votes2
answers49
viewsA: Correlation test on categorical data with low number of counts
The number of cases is very low in several boxes (many contain the value 0, for example). The message of alert when you apply the chisq.test is: Warning message: In chisq.test(Table2) : Chi-Squared…
-
1
votes1
answer70
viewsQ: navbar overwrites the other elements when the page size is changed
I have this fictitious application in R. I tried to adjust the .navbar, .sidebar and the .value-box with the property position: relative; to avoid overlapping of elements: .navbar { position:…
-
0
votes1
answer146
viewsQ: Click and make options disappear with CSS
I click on an option from a navbar options box, the page opens, but this options box remains with the mouse over the chosen option: Before clicking: After clicking: I want after the click, the…
-
-2
votes1
answer128
viewsQ: Position element in the middle of the screen
I tried to adjust an element (sliders) in the middle of the screen, but I couldn’t do it: Meo aims to leave this image always in the center, right, no matter the monitor being used by another user.…
-
3
votes2
answers87
viewsA: Create a matrix without repeating the values of the date argument
This is what occurs in R is called recycling (repetition of values to complete the vector). You can solve this situation by adjusting the vector outside the function matrix or inside it. Here are…
-
0
votes2
answers304
viewsA: How to filter data from R lines?
Solution tidyverse + lubridate: library(tidyverse) library(lubridate) selic %>% mutate(var_0 = '01') %>% unite(data = ., var_0, var_1, var_2, col = 'new', sep = '-', remove = FALSE) %>%…
-
1
votes2
answers70
viewsQ: Loop for inside a list with a group argument function
I have this data: df_1 <- data.frame( x = replicate( n = 3, expr = runif(n = 30, min = 20, max = 100), simplify = TRUE ), y = as.factor(sample(x = 1:3, size = 30, replace = TRUE)) ) And this…
-
2
votes1
answer58
viewsQ: Doubt with loop operations
I have this df: df_1 <- data.frame( x = replicate( n = 6, expr = runif(n = 30, min = 20, max = 100), simplify = TRUE ), y = as.factor(sample(x = 1:3, size = 30, replace = TRUE)) ) I would like to…
-
3
votes1
answer77
viewsA: How to save Twitter data, downloaded in R, in Excel?
Try with writexl install.packages('writexl') library(writexl) write_xlsx(x = trump, path = 'trump.xlsx')
-
1
votes2
answers74
viewsA: Select dataframe information based on specific conditions
Solution tidyverse: library(tidyverse) dados %>% group_by(Municipio) %>% mutate(var_1 = n()) %>% mutate(var_2 = n_distinct(Genero)) %>% filter(var_1 == 2 & var_2 == 2) %>%…
-
4
votes3
answers174
viewsA: A: Column average over the range of values in R
Suppose the database is df and the variable IDH: set.seed(123) df <- data.frame( IDH = runif(n = 100, min = .250, max = 1) ) With tidyverse the solution is simple: df %>% filter(between(x =…
-
4
votes1
answer183
viewsQ: Difference between the metacharacters b and B
The metacharacters \b and \B sane anchors that marks the boundaries of a string (where it starts, ends, or both). Works by the ASCII standard, containing only [A-Za-z0-9_]. That is, only parts of…
-
1
votes2
answers93
viewsQ: Regular expression by string index
Consider this variable: x <- c('horccaeon', 'coleon', 'volues', 'mol', 'nao', 'tom', 'nada', 'auio', 'aqoio') I used the following regex to extract strings with the second letter o:…
-
4
votes1
answer665
viewsQ: How does the metacharacter t work in a regex?
I have this variable: y <- c('stack overflow', 'user number 2', 'nova\nlinha', 'nova \n linha') And these functions with their results: library(tidyverse) With \n: str_detect(string = y,…
-
1
votes7
answers1523
viewsA: How to calculate the median of a line in a date.frame in R?
Using functions of the package purrr you can do analysis byrow in a simpler way. Also, knowing that the median can only be calculated in numbers, you can analyze the vectors by their classes. Taking…
-
3
votes3
answers309
viewsA: How to replace variables with NEGATIVE values with their ABSOLUTE VALUE within a date.frame in R?
Use the function abs (Absolute value). Since the module (absolute value) can only be calculated on numbers, the most coherent solution is to analyze by the classes of vectors present in Dados.…
-
0
votes6
answers31940
viewsA: How to remove a data.frame column in R?
All answers given solve the problem. But, consider this: a database with many variables (e.g. 50 +); there are multiple classes of variables (numeric, character...); that these variables are out of…
-
2
votes2
answers909
viewsA: How to replace variables with NEGATIVE values with ZERO within a date.frame in R?
With only R base it is possible to do this. I will present two solutions using ifelse: data_1 <- sapply(X = Dados[c(2:6)], FUN = function(x) { ifelse(test = x < 0, 0, x) }) data_1 A B C D E…
-
0
votes3
answers55
viewsA: Convert a factor (of real numbers) into a numerical vector
The @Willian solution meets the order. An alternative would be to resolve with tidyverse: library(tidyverse) df %>% mutate(var = as.numeric(str_replace_all(string = confianca, pattern = ',',…
-
1
votes3
answers55
viewsQ: Convert a factor (of real numbers) into a numerical vector
I know there’s such a thing question. But in my case, there’s a difference: want to convert the factor into a numerical vector without removing the separator (either semicolon). Dice: df <-…
-
4
votes1
answer139
viewsA: Perform mutate in columns simultaneously
You can do it like this: library(tidyverse) df %>% mutate_at(.vars = vars(contains('col')), .funs = funs(. = percentualize)) col1 col2 col3 col1_. col2_. col3_. 1 1 4 7 1% 4% 7% 2 2 5 8 2% 5% 8%…
-
4
votes2
answers89
viewsQ: Rearview Metacharacter x does not take the corresponding groups when I change the order of these
The rear-view metacharacter \x repeats something captured in some group () previous in regex. For example: library(stringr) a <- 'quero-quero' str_extract(string = a, pattern = regex(pattern =…
-
16
votes1
answer250
viewsQ: Difference between the greedy ?? and *quantifiers?
I have these strings: x <- c('ondasffasf', 'ondassn\nlld', 'ondas', 'ond', 'ndasss', 'das') And this code with ??: library(stringr) str_extract(x, regex('ondas??')) [1] "onda" "onda" "onda" NA NA…
-
1
votes1
answer802
viewsQ: Pseudoclass :not() does not work with combinator
I have this code html: <article> <p class="ola"> cafe </p> <p>Primeiro parágrafo</p> <p>Segundo parágrafo</p> <aside> <h1>Informações</h1>…