Posts by neves • 5,644 points
165 posts
-
1
votes1
answer48
viewsA: Searching for specific names within lines
You can do it: partido<-c('PT/PSDC/PCB/PMDB') library(stringr) str_extract(partido,'PMDB') This function extracts the first element found (in this case, only a unit of PMDB). If there are several…
-
5
votes3
answers969
viewsQ: Differences and similarities between apply and for loop functions
I have this list: dataset<-data.frame(matrix(runif(6*30,20,100),ncol=6)) cluster<-kmeans(dataset,centers=3) cluster dataset$kmeans<-as.factor(cluster[['cluster']])…
-
2
votes2
answers72
viewsQ: Function `Broom::Tidy` does not produce the output for some functions
I have the data.frame: library(tidyverse) library(broom) set.seed(1) dataset<-as_tibble(matrix(runif(6*30,20,100),ncol=6)) cluster<-kmeans(dataset,3)…
-
9
votes1
answer161
viewsQ: Difference between Function Factory and closure
In the book Advanced R, to chapters 10 and 11, the author defines Function Factory as: "a Factory for making new functions" Translation Google Translate: a factory to do new functions. And closure…
-
5
votes3
answers155
viewsA: Error with variable naming functions in a list
A solution with dplyr would be: library(dplyr) map(mylist,~rename_at(.,vars(contains('EaN')),~'variable')) contains has action similar to stringr::regex with ignore_case=TRUE Withdrawn (and adapted)…
-
6
votes3
answers155
viewsQ: Error with variable naming functions in a list
I try to run a function to rename variables from data.frames of a list and get only error messages. When I run the function for a database the analysis takes place: library(tidyverse)…
-
3
votes2
answers504
viewsA: Replace ")" by space using str_replace_all() in R
In stringr you need to insert [] within the functions for the character to be recognized. This would look like this for the variable telefone: library(stringr)…
-
14
votes1
answer261
viewsQ: What is the usefulness of lexical and dynamic scopes in R?
In the chapter 6 of the book Advanced R there are definitions of lexical and dynamic scopes. However, I did not understand the usefulness of knowing these concepts in r. Thus, the usefulness of the…
-
4
votes1
answer202
viewsQ: Functional programming: applicability of parameters (.x) and (.) in purrr::map
I’m migrating to the package purrr, mainly in relation to the use of the function map. But I’m having a hard time understanding the concepts of functional programming in purrr. I would like to learn…
-
4
votes3
answers186
viewsQ: Remove Environment (and list) elements based on their classes
Consider the vectors: a<-1:10 b<-1:10 c<-'a' d<-'a' e<-list('b') f<-list('b') g<-1.1 h<-1.1 The function below removes only one class (which is integer, in this case):…
-
8
votes2
answers164
viewsQ: Remove all Environment elements containing numbers and uppercase letters
Consider the objects: vectorA<-1:10 vectorU<-1:10 vector<-1:10 vector1<-1:10 vector86<-1:10 How to remove objects containing numbers? How to remove objects containing uppercase…
-
5
votes2
answers214
viewsQ: Application of the `assign` function in loops
I want to play names in variables with a loop. With for I can get: library(tidyverse) for(i in 1:6){ names<-str_c('var',i) assign(names,runif(30,20,100)) } But with lapply and map nay: lapply…
-
2
votes1
answer334
viewsQ: Function`recode` (dplyr) does not accept numerical ranges
Consider the vector: x<-runif(30,20,100) I would like to categorize this vector with the function recode of package dplyr. Ranges can be arbitrary. For example: from 20 to 50 = 1 from 51 to 75 =…
-
2
votes1
answer123
viewsQ: How to apply the `Join` (dplyr) function in a list?
I tried in many ways to apply the functions join of package dplyr in a list but I can’t do this. With the function Reduce and merge This is easy to do, but my intention is to do it with join as this…
-
19
votes1
answer2245
viewsQ: R is an object-oriented language?
A theoretical doubt of who uses (and who does not use) the r is whether this programming language is (or not) object-oriented. I’ve seen comments on Soen that said the following: r is not object…
-
1
votes2
answers61
viewsQ: Remove items from a list that are part of a character or part of a certain class
Consider the list: mylist=structure(list(quem = 1:10, quer = c(1.1, 2.1, 3.1, 4.1, 5.1), isso = structure(list(X__1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), X__2 = c(123, 456, 789, 1122, NA,…
-
4
votes1
answer1077
viewsQ: Execution of multiple functions within loops *pply
dput: dataset=structure(list(var1 = c(93.6192987300456, 89.1180437617004, 73.1943506933749, 99.0350881963968, 32.3240431956947, 94.2517145164311, 53.7740966491401, 64.1642983071506, 83.643307145685,…
-
5
votes2
answers94
viewsA: Different Records
I don’t know how you used the function anti_join, but she worked here: library(dplyr) anti_join(dados_1,dados_2,by='MATRICULA_A') # 'MATRICULA_A' é a variável em comum MATRICULA_A 1 123 2 234…
-
3
votes1
answer118
viewsQ: Cluster analysis by groups
I’m trying an analysis of cluster for several groups within a dataframe, with the aim of returning the characteristics of this analysis (ex. the resulting groups) in a database through the function…
-
6
votes3
answers397
viewsA: How to categorize values in a Data Frame in R?
Since you did not provide the categorization ranges, I have subjectively stipulated them to answer. The function recode, of the package car, solves the problem: library(car)…
-
2
votes2
answers2546
viewsA: How to view comments on Rstudio console?
Another option may be to attach a comment to any object and add it to the Console. Example with a vector (x) whichever: x<-1:10 comment(x)<-'My vector' comment(x) [1] "My vector"…
-
7
votes2
answers408
viewsQ: Builder and inheritance in R
It is a theoretical question, but it may help to understand a little the logic of programming in r. What it is and how it works builder in the language r? What is its use? What it is and how it…
-
7
votes1
answer201
viewsQ: Meaning of "code acting on data" and "data controlling access to code"
This is a purely theoretical question about programming. I would like to know from an example simple (response with complement of figures, drawings, etc.), the meaning of two sentences for two types…
-
3
votes1
answer92
viewsQ: How to delete elements from the list through a condition?
I used the following function: splitfile<-split(training,list(training$group1,training$city)) and this gives me a list of dataframes with different lines, based on the variables I selected.…
-
2
votes1
answer128
viewsQ: Export multiple. csv files in R
I’m trying to export multiple files .csv in R through the function lapply. These files are within a list. I tried the following settings: lapply(list,function(x)…
-
2
votes1
answer136
viewsQ: Which object identifier should I use?
There are several functions that serve to identify objects in the R (or at least "think" they are for this purpose), such as: class() mode() typeof() storage.mode() str() that, with a simple…
-
5
votes1
answer1124
viewsQ: What is the difference between Numeric and integer vectors?
In R, identical vectors (containing the same numbers) can assume the classes numeric and integer. For example: x<-(1:5) class(x) [1] "integer" x<-c(1,2,3,4,5) class(x) [1] "numeric" Why does…
-
1
votes1
answer122
viewsQ: How to return the most prevalent category associated with a group?
I have a database, in which the variable a is the group variable and b a variable with some categories. My goal is, within each group of a, return what else appears in b. Consider the dput:…
-
3
votes1
answer369
viewsQ: How to run str_detect (stringr) for more than one variable and at once?
I want to filter my database based on two variables: via and city. This filter, however, is made by means of particles of cases present in these two variables. For example, I want to analyze who…
-
2
votes1
answer1261
viewsQ: Remove duplicate cases and keep specific values from another variable
Consider the following situation: I have a database with two variables. The first is a variable with duplicate values (e.g. Cpfxxx.xxx.xxx-xx appears 14 times, Cpfxxx.xxx.xxx-xx appears 18 times and…
-
2
votes1
answer50
viewsQ: Problems with indexing in cbind
I try to perform the function cbind as follows: reg<-lm(cbind(variable1,variable2,variable3,variable4,variable5,variable6)~kmeans,data=dataset) and it works. But consider this structure:…
-
3
votes2
answers64
viewsQ: Automatically add all Environment elements to a list
Suppose my Environment contains the following objects (fictitious names): abcd # numeric efgh # dataframe ijkl # matrix mnop # character My goal: put them in a list automatically without having to…
-
2
votes1
answer73
viewsQ: Error with lapply function
I try to perform the following function: result<-lapply(mylist,function(x)cbind(x,var=tapply(x[,c(3)],x[,c(1)],sum))) But this mistake arises: Error in data.frame(..., check.names = FALSE) :…
-
3
votes1
answer144
viewsQ: How to remove lines based on the values of another variable?
Consider the dataframe: data<-data.frame(a=c(1,3,4,5,6,NA,6,NA),b=c(1,NA,NA,4,6,7,NA,1)) I want to eliminate the whole line when it exists NA in variable 'a'. So what I hope is: data a b 1 1 1 2…
-
3
votes1
answer47
viewsQ: Make the variables created with lapply be allocated in their respective dataframe within a list
Use the function lapply and wish the results to return within their respective dataframe within the list. Consider the following function:…
-
2
votes2
answers151
viewsQ: Perform the tapply function for multiple variables on a dataframe (with pairwise.t.test)
I try to perform the function tapply for several variables at the same time, but using the function pairwise.t.test. However, I do not succeed. So I have to do this:…
-
3
votes1
answer86
viewsQ: What is the logic of Function(x) and Function(x,y) in R?
In the execution of loops with functions apply, is always used function(x). Example: lapply(df,function(x)gsub('[^A-Z]','',x)) In other functions, however, it is used function(x,y). Example:…
-
-1
votes1
answer373
viewsQ: Insert variable names in the first line of the dataframe
I want to insert the name of the variables as the first line of my datafrane. For example: X11.3116555055603 X16.8234904296696 1 14.79609 10.37313 2 10.02141 16.38942 3 12.10137 18.75196 4 14.29997…
-
2
votes1
answer58
viewsQ: Preserve rownames after use of apply family functions
I make use of the functions apply and, I need you to rownames stay in the database after execution of these. Consider: dataframe=structure(list(Composição100g = structure(c("Valor energético (KJ)",…
-
2
votes4
answers7118
viewsA: Changing the name of a variable in a dataframe R
You can do that too: colnames(dados)[1]<-'Pais' # 1 é o número da coluna Country
-
6
votes2
answers639
viewsQ: How to split the dataframes of a list based on a group variable, common in all of them?
I have a list with n dataframes. They have a common variable, called group. I want to analyze the dataframes only with the groups a and c of group. My goal: to return, within the list, these…
-
1
votes1
answer248
viewsQ: Insert rownames/ values as a new variable in a list dataframes
Suppose n dataframes within a list. Each of these dataframes has different lines. For example, in a dataframe, rownames are characters (make of cars) and in other, rownames These are hospital…
-
2
votes1
answer58
viewsQ: Lapply does not return the desired result for some functions
My list: structure(list(col1 = structure(list(a = 1:5, b = 1:5, c = 1:5), .Names = c("a", "b", "c"), row.names = c(NA, -5L), class = "data.frame"), col2 = structure(list( a = 6:10, c = 6:10), .Names…
-
2
votes1
answer39
viewsQ: Use lapply (replacement for) to leave only columns in common for multiple dataframes in a new list
dput of the list: structure(list(col1 = structure(list(a = 1:5, b = 1:5, c = 1:5), .Names = c("a", "b", "c"), row.names = c(NA, -5L), class = "data.frame"), col2 = structure(list( a = 6:10, c =…
-
2
votes2
answers711
viewsA: Issue discriminant function constant - linear discriminant analysis [R]
After performing the discriminant analysis with the package MASS, you can get the constant. The expression is: groupmean<-(model$prior%*%model$means) constant<-(groupmean%*%model$scaling)…
-
2
votes1
answer80
viewsQ: Error in R with list2env function
I try to convert my list (which contains 12 dataframes and approx. 20,000 lines each) into separate dataframes: list2env(mylist,envir=.GlobalEnv) but, the following error message appears: names(x)…
-
2
votes2
answers119
viewsQ: Convert all Environment files to '.Rda' and then load them at once with a loop
I want to convert ALL dataframes of my Environment into file type .Rda. My goal is, after this action, to restart the R and load all of them at once with a loop for.
-
1
votes2
answers116
viewsA: Replacing for the Lapply function in R
Execute: lista1<-list(c(1, 2, 3, 4, 5), c(6, 7, 8, 9, 10), c(11, 12, 13, 14, 15 ), c(16, 17, 18, 19, 20), c(21, 22, 23, 24, 25)) novalista<-lapply(lista1,'*',8) novalista #[[1]] #[1] 8 16 24…
-
5
votes2
answers914
viewsA: Rstudio. Colors in a set of Boxplots
To make the analysis more organized, you can do the following: 1) create a data.frame with all its variables. But, as each vector has different sizes, do: library(qpcR)…
-
5
votes2
answers229
viewsA: Text mining with R (stringr)
Fictional example, which may be reproduced for your case: x<-c('Bem-vinda ao Stack Overflow em Português') library(stringr) str_sub(x, 2, -10) # as aspas do texto também contam como strings. Por…