Posts by Rui Barradas • 15,422 points
432 posts
- 
		4 votes2 answers214 viewsA: Application of the `assign` function in loopsThe assign creates new variables in the environment of the anonymous function, not .GlobalEnv. That’s what the argument’s for envir. library(tidyverse) set.seed(1234) for(i in 1:6){… 
- 
		6 votes2 answers114 viewsA: How to create one vector from another in r?Another way is with the function is.na<-. And to create the vector fixo used the function numeric, more the idea by Willian Vieira to obtain the length of fixo with max(coef). coef <- c(1, 4,… ranswered Rui Barradas 15,422
- 
		5 votes1 answer146 viewsA: histrogramSee if the following is what you want. I included the package ggpubr to rotate species names on the axis x. library(ggplot2) library(ggpubr) ggplot(dados, aes(specie, depletion_rate)) +… 
- 
		4 votes2 answers66 viewsA: read txt file with less than 5 elements using read.tableKnowing what is in the file, the following works. It is absolutely nothing general. dados <- read.table(file = "Artur.txt", sep = "|", comment.char = "+", skip = 4, fill = TRUE) dados <-… 
- 
		3 votes2 answers87 viewsA: Separate data from a variableThis function separates the city and state even if the city has a hyphen '-' and has as output a list with cities and states. The vector for testing has a fictitious city, with hyphens where in… ranswered Rui Barradas 15,422
- 
		4 votes1 answer334 viewsA: Function`recode` (dplyr) does not accept numerical rangesThe easiest way to do this is on R basis with the function findInterval. The function dplyr appropriate will be case_when, nay recode. Here are the two ways. library(dplyr) set.seed(1234) x <-… ranswered Rui Barradas 15,422
- 
		12 votes2 answers823 viewsA: What is the difference between [] and [[]] in the R?I’ll start by quoting the R’s documentation: The Most Important Distinction between [, [[ and $ is that the [ can select more than one element whereas the other two select a single element.… ranswered Rui Barradas 15,422
- 
		3 votes2 answers1179 viewsA: Weighted Average in RHere are two ways to calculate averages for groups of c1 and c2. R base. The function aggregate is ideal for this. It’s simple, it solves the problem in a line of code. aggregate(c3 ~ c1 + c2,… ranswered Rui Barradas 15,422
- 
		3 votes2 answers61 viewsA: Remove items from a list that are part of a character or part of a certain classHere are two functions. Objective 1 qu_extract <- function(object){ if(!inherits(object, "list")){ obj <- deparse(substitute(object)) stop(paste(obj, "não é de classe 'list'")) } inx <-… 
- 
		3 votes1 answer175 viewsA: Extracting data from a data frame in RCan divide data with the function split and then apply column extraction to each sub-df. sp <- split(data, data$INSTÂNCIA) result <- lapply(seq_along(sp), function(i){ sp[[i]][, i + 1] })… 
- 
		6 votes2 answers85 viewsA: Repeating the subtraction of groups in a data frame for all numerical variablesThe following does what it wants with R base. ib <- which(df$grp == "b") ic <- which(df$grp == "c") df[3:5] <- lapply(df[3:5], function(x){ x[ib] <- x[ib] - x[ic] x }) df # grp index… ranswered Rui Barradas 15,422
- 
		3 votes3 answers433 viewsA: geom_text positioning labels individuallyTry the following. The trick is to find a multiplier for the distance to add to the position y in the geom_text, in this case 2. I’ll do it with ave. mult <- ave(dat$valor, dat$ano, FUN =… 
- 
		2 votes2 answers94 viewsA: Different RecordsOn R base can do this in several ways. With setdiff: setdiff(dados_1$MATRICULA_A, dados_2$MATRICULA_A) #[1] 123 234 With %in% is more flexible, can either obtain a vector or a class object… 
- 
		5 votes3 answers397 viewsA: How to categorize values in a Data Frame in R?Yet another way is with the function findInterval, which I believe is in this case better than the function cut. If we have new vectors niveis and points limite the following solves the problem.… 
- 
		4 votes1 answer387 viewsA: Subtract rows from a group in a data frame by another groupI believe the following code is simpler and is equivalent. library(dplyr) df2 <- df %>% filter(grupo %in% c("A1", "A1.2")) %>% # grepl("A1", grupo) mutate(grupo = "Am") %>%… 
- 
		5 votes1 answer89 viewsA: Split data frame by the first line itselfThere are several ways to do what you want. I will use two of them. With the tidyverse. Only on R basis. In both I will work with copies of DF1. First tidyverse. library(tidyverse) DF2 <- DF1… ranswered Rui Barradas 15,422
- 
		7 votes1 answer90 viewsA: How to read txt file with multi-dimensional datasets?The following function does what it wants, at least with the question data. I tried to make the function as general as possible but one never knows. ler_txt <- function(file, path = "."){ if(path… ranswered Rui Barradas 15,422
- 
		4 votes1 answer92 viewsA: How to delete elements from the list through a condition?It’s not as difficult as that to eliminate all data frames with zero lines at once. It’s even easier than it looks, just an instruction. Explained by parties. First, I’ll use the sapply and not the… ranswered Rui Barradas 15,422
- 
		3 votes1 answer212 viewsA: How to ignore an error in R?There are several ways to process errors available in R, perhaps the most frequently used is the function tryCatch. Erro <- function(valor){ valor <- valor + 1 print(valor) valor <- valor +… 
- 
		1 votes2 answers70 viewsA: How to return a logical value when the lines have identical values in the R software?The following function solves the problem both for numerical data (dados1) as for non-numerical data, for example class character (dados2). todosIguais <- function(DF){ apply(DF, 1, function(x)… 
- 
		1 votes2 answers144 viewsA: cycle for calculation of a functionHere is a cycle for very simple. And the comparison with the result of the answer from Carlos Eduardo Lagosta. suaFuncao <- function(y, x) sum(y^(0:x)) suaFuncao2 <- function(y, x){ s <- 1… ranswered Rui Barradas 15,422
- 
		4 votes2 answers121 viewsA: calculate a probability function in RTo sample a discrete variable that takes a finite number of values, one can use the base function sample. Before running the function sample or another function that generates pseudo-random numbers,… ranswered Rui Barradas 15,422
- 
		2 votes1 answer94 viewsA: Generation of samples in RConsidering the way you’re choosing amostra1, the natural way to get the other data is or with %in% and which or with match. First I’ll create a vector dados. library(car) set.seed(7437) # Torna os… ranswered Rui Barradas 15,422
- 
		2 votes2 answers45 viewsA: Selection/Cleaning of information in a columnI believe that in the question complicated the regex too much. See thus. First be alone with what’s between ( and ). As these characters are special characters you need to use \\( and \\). That’s… 
- 
		2 votes2 answers253 viewsA: File Import in R - Taking only part of the nameThe easiest way is with the argument pattern of functions list.files or dir. As the files are in another directory, first switch to it. old_dir <- setwd("C:/tmp/") fich1 <- list.files(pattern… ranswered Rui Barradas 15,422
- 
		5 votes1 answer46 viewsA: "Less" value is removed in RHere’s a solution with the package lubridate. Start by defining a function, negativo, which only modifies the values with the format "HH:MM:SS" where at least one of these numbers is negative. Then… ranswered Rui Barradas 15,422
- 
		3 votes1 answer117 viewsA: Loop Error in RThe error message can be solved with tryCatch. Also I made other changes to your code. Now the cycle while stores the results of all iterations on list result, not only the last. library(forecast) a… 
- 
		5 votes2 answers48 viewsA: Matrix of ExpressionThe easiest way is with the function sign. Simpler can’t be: sign(dados) # FC12h FC10d FC6w #1 -1 -1 0 #2 -1 1 1 #3 -1 1 -1 Dice. dados <- read.table(text = " FC12h FC10d FC6w -8.44770875… 
- 
		1 votes2 answers64 viewsA: Automatically add all Environment elements to a listHere’s a way to solve the problem. First I will create an Environment with the objects described in the question. set.seed(1234) e <- new.env() e$abcd <- rnorm(10) e$efgh <- data.frame(A =… 
- 
		4 votes1 answer811 viewsA: How to make loops in RThere’s no reason to wear cycles for or others. R is a vector programming language, which means that it can process integer vectors at once. Here it goes. First I will transform the column vector… 
- 
		3 votes1 answer136 viewsA: Creating a dataframe based on two other dataframes using dplyr in RHere it comes. The trick is to group by the first characters of names(finaldf) and then process the group list. First we chose the names that matter. nms <- names(finaldf) nms <-… 
- 
		3 votes1 answer47 viewsA: How to create a list of values with restriction on a stipulated number of dataSee if the following function is what you want. The results are in line with the expected results of the question. valores <- function(obs, n = 130, incr = 20, incr2 = 100, comprimento = 5){ pl… 
- 
		5 votes1 answer59 viewsA: Reorganize data frame in a list using dplyr in RI don’t know if this is what you want, but the result res has a format similar to that of the question. sp <- split(df[3:7], df$ind) res <- lapply(sp, function(s) lapply(s, unique)) str(res)… 
- 
		3 votes1 answer58 viewsA: Preserve rownames after use of apply family functionsThe problem of your code, and losing the attribute names is that it’s complicating what’s much simpler. First call, data.frame() is a function and therefore to assign a value to a function argument… 
- 
		4 votes1 answer44 viewsA: Filling lines with correct data in R, joining successive linesA possible solution is the following. Uses a package function zoo to modify the column a and then finished back to the zeroes where they were. zeros <- a$a == 0 is.na(a$a) <- zeros a$a <-… ranswered Rui Barradas 15,422
- 
		5 votes1 answer84 viewsA: How to create a column with specific conditions?The following solves the question with lapply/split. Divide the table dados for cod with the function split and an anonymous function is applied to each sub-dataframe. res <- lapply(split(dados,… ranswered Rui Barradas 15,422
- 
		2 votes2 answers632 viewsA: Ks.test and p-value < 2.2e-16First of all, I think you’re getting off on the wrong foot. It should not decide at the outset that it will compare the distribution of the data with such and such parametric distributions. Should… ranswered Rui Barradas 15,422
- 
		3 votes1 answer47 viewsA: Poorly calculated coefficients in Linear Regression in R due to NA sThere must be something non-standard with your R session. How to read in help("lm"), in section Arguments (my emphasis): in action. a Function which indicates what should happen when the data… ranswered Rui Barradas 15,422
- 
		4 votes2 answers116 viewsA: Replacing for the Lapply function in RFirst, R is a vector programming language. It means the double cycle for is not necessary. As each member of the lista1 is a vector, you can multiply the integer vector by 8. for (i in 1:5) {… ranswered Rui Barradas 15,422
- 
		5 votes2 answers229 viewsA: Text mining with R (stringr)From the @Giovani response, I wrote a small function to solve the difference problem between what str_sub does and what the question asks. On the page help("str_sub"), section Details: Details… 
- 
		3 votes2 answers160 viewsA: Renaming all Environment objects with a specific nameA way to rename the objects of a environment may be as follows. renameObject <- function(old, new, envir){ for(i in seq_along(old)){ assign(new[[i]], get(old[[i]], envir = envir), envir = envir)… 
- 
		3 votes2 answers96 viewsA: Convert a list with different line dataframes into independent dataframesDespite the function createdf from @Daniel work well, the base R already has a function that makes it, the function list2env. Creating the same list a in a new R session: ls() #character(0) Now the… ranswered Rui Barradas 15,422
- 
		2 votes3 answers1792 viewsA: Columns with numbers of different rows, how to join?Multiple bases can be joined only with R base. As the function merge only takes two data.frame'when there are more than two have to be put in a list and the function is made Reduce call the merge.… ranswered Rui Barradas 15,422
- 
		3 votes2 answers97 viewsA: Automate number creation in a loop in RWhen you have several similar objects, the general rule is to have them in a list. Instead of having n (in this case 10) objects in the .GlobalEnv you only get one. To create this list no cycle is… 
- 
		2 votes1 answer131 viewsA: Generate splitter list of a given number nTo solve this problem just use which(vetor lógico). Note that I have made two other modifications. I have included the newline to finish the cat and now the function is not limited to printing… ranswered Rui Barradas 15,422
- 
		4 votes2 answers651 viewsA: How to perform the tapply function for multiple dataframes in R?First, it’s best to have all the bases on a list. I’m going to do this with a combination of ls and mget. ls(pattern = "^dataset\\d+$") #[1] "dataset1" "dataset2" "dataset3" df_list <-… 
- 
		1 votes2 answers1914 viewsA: Argument is neither numerical nor logical: returning NAIn addition to the @Fernandes suggestion, which you should follow, there is another problem. If R is reading values as strings there should be non-numerical values in the file. One way to solve this… 
- 
		1 votes1 answer145 viewsA: How to convert hexadecimal to binary in a matrix in r?The following function may do what you want. First the data. m <- matrix(c("FA", "F8", "D0", "CE", "17", "6A", "0E", "D6", "22"), nrow = 3, byrow = TRUE) Now the code. library(BMS) hex2matrix… 
- 
		2 votes2 answers68 viewsA: Replacing text with words from another fileOnly with R base can it be done as follows. pat <- paste0("\\b", Arquivo2$Termo, "\\b") for(i in seq_along(pat)) Arquivo1$Texto <- gsub(pat[i], Arquivo2$Termo_pai[i], Arquivo1$Texto) Arquivo1… 
- 
		1 votes1 answer40 viewsA: Calculate the variance of the values related to a group interval? In soft RWhat you ask for can be done with family functions *apply, after a function has been defined to calculate the statistics of interest. estatisticas <- function(x, na.rm = TRUE){ m <- mean(x,… ranswered Rui Barradas 15,422