Posts by rowang • 2,343 points
35 posts
-
6
votes2
answers1558
viewsQ: How to know the day of the week in R?
Is there a package or function in R that allows to easily obtain the day of the week from dates? datas <- c("2010-06-28", "2011-05-25", "2010-09-28", "2011-12-05", "2010-09-14", "2010-01-01",…
-
5
votes4
answers8280
viewsA: How to put the regression line on a graph?
Another way is to use the function curve. curve(coef(modelo)[1]+coef(modelo)[2]*x,add=TRUE,col = "blue",lwd=2) An advantage of this function is that it also serves if the model involves non-linear…
-
2
votes0
answers50
viewsQ: How to remove accents in R?
How to efficiently remove accents and tilde in the R? What I want to do is to do it in a more practical way than to specify the substitution letter by letter. estados=c("São…
-
5
votes4
answers2494
viewsQ: How to join observations of tables that have a different set of variables in R?
In the case below id corresponds to observations contained in two different data frames, in df1 the observations have the variable x and in df2 the observations have x and z In these cases how to…
-
7
votes1
answer272
viewsQ: How to know the arguments contained in '...' in a function in R?
In R can be used '...' so that the function receives an undetermined number of arguments. How to know which arguments were used in the function? Example, if I wanted to print the arguments used.…
-
2
votes2
answers1624
viewsA: Reversing Order column data.frame
Recreating your database df<- read.table(text="data spread 1 2006-01-01 -3.0404577 2 2006-02-01 -3.3902628 3 2006-03-01 -2.5283960 4 2006-04-01 -1.5279234 5 2006-05-01 -0.0897918 6 2006-06-01…
-
3
votes2
answers2993
viewsA: Eliminating Double Lines from a Data.Frame
Another way to do this is by using the function duplicated. Using the base recreated by Carlos. df<- read.table(text = "values ind 1 10.82000 2011-01-03 2 11.75000 2011-01-03 3 10.82000…
-
7
votes2
answers969
viewsQ: How to do an optimization with inequality restriction?
Suppose I want to minimize the following function: -(5-(x1-2)^2-2*(x2-1)^2) s.a. x1+4*x2 < 3 For optimization problems without restriction I can use the following code. fr <- function(x){ x1…
-
5
votes5
answers12797
viewsA: How to consolidate (aggregate or group) the values in a database?
Another option is to use sqldf library(sqldf) sqldf("SELECT vendedor, sum(vendas) from df group by vendedor") sqldf("SELECT vendedor, regiao, sum(vendas) from df group by vendedor, regiao")…
-
2
votes2
answers178
viewsA: Collapsing texts in a single line in a database
You can also use the sqldf package library(sqldf) tabela2 <- sqldf("SELECT ano, count(nome) as quantidade, group_concat(nome) as nomes FROM tabela GROUP by ano")…
-
6
votes2
answers443
viewsQ: Reorder categories in a data frame
When we import data into R it sorts the categories alphabetically. How to change this order? Suppose these are the following data: df <- data.frame(categorias=c("Muito…
-
5
votes1
answer497
viewsQ: How to remove unused categories (levels) in the database
Suppose I have the following database: df <- data.frame(categorias=c("A","B","C","D","E"), valores=seq(1:5)) When I do a subset of that data frame the categories I removed continue. subdf <-…
-
5
votes2
answers178
viewsQ: Collapsing texts in a single line in a database
Suppose I have the following data tabela<-structure(list(nome = structure(c(2L, 9L, 6L, 1L, 8L, 3L, 4L, 5L, 7L, 10L, 11L), .Label = c("12 Anos de Escravidão", "A Caça", "Ela", "Gravidade", "O…
-
5
votes2
answers302
views -
2
votes4
answers19992
viewsA: How to transport data from R to excel?
Can use: library(xlsx) write.xlsx(nomeObjeto,file="nomeObjeto.xlsx")
-
15
votes3
answers24782
viewsQ: How to sort an array by values?
Suppose I have the following data. Data = [3,5,1,7,3,9,10]; If I try to use the Sort method in this array the sort is done as if the data were not numerical. Data.sort() But the data type is…
javascriptasked rowang 2,343 -
5
votes1
answer10722
viewsQ: How to plot a line chart with different colors depending on the value?
Suppose the following data: set.seed(1) y<-rnorm(101) x<-seq(from=0, to=100,by=1) I want to make a Plot with a line that has different color for negative values. To make a chart of points just…
-
8
votes2
answers10996
viewsA: Error while converting numbers. How to convert factors to numbers?
If you analyze the structure of the object you will see where the problem occurs: str(unclass(dados$x)) atomic [1:4] 2 1 4 3 - attr(*, "levels")= chr [1:4] "10" "11" "15" "20" The object dados$x is…
-
9
votes3
answers1784
views -
11
votes3
answers2755
viewsQ: How to assign the results of a function that returns a list of objects?
In R we can make a function return more than one object through a list. But how to assign these objects to two distinct variables? Example: f<-function(){ primeiro<-1:10 segundo<-11:21…
-
12
votes1
answer2292
viewsQ: How to create objects (variables) with different names within a loop?
I want to generate different databases in a loop. In the example below would be 3 distinct databases with the following names: "data1", "data2", "data3". for (n in 1:3){ dados<-paste0("dados",n)…
-
7
votes2
answers4567
viewsQ: How to turn a string into Date format in R?
In the code below the dates are in the format: Mes Dia Ano. I need R to recognize the variable as date. #importa os dados library(XML)…
-
5
votes3
answers693
viewsQ: How to know the amount of NA in each variable?
Suppose I’m working with the following database: df=data.frame(v=c(1,2,NA,4,NA,6,7,8,9,10),v2=c(11,NA,NA,14,NA,16,NA,NA,19,NA), v3=c(21,22,23,24,25,26,27,28,29,30), v4=c("a","b","c", NA,…
-
22
votes5
answers12797
viewsQ: How to consolidate (aggregate or group) the values in a database?
Suppose I have the following database vendas<-c(100,140,200,300,20,1000,200,3000) vendedor<-c("A","B","A","B","C","C","D","A")…
-
3
votes1
answer5683
views -
4
votes3
answers6503
viewsQ: How to avoid encoding problems when picking up data with Twitter?
I’m taking Twitter data with the package twitteR for r but the tweets are coming with encoding. Someone knows how to get around this problem? library(twitteR) library(stringr) library(ROAuth)…
-
7
votes1
answer4682
views -
5
votes2
answers637
viewsA: How to make a conditional y~x graph for each data.frame factor?
You can also use the Lattice library for this. library(lattice) xyplot(y~x|w, data=dados)…
-
5
votes1
answer363
viewsQ: Continue running the loop even if a passage gives problem
I’m downloading Bovespa’s stock data for the package quantmod. However, I still do not know why, in the data from Santander (SANB11) the function getSymbols package is giving problem and loop for…
-
5
votes1
answer3442
viewsQ: How to plot the estimated logistic regression model
Suppose I have the data below, apply a logistic regression to them and want to visualize the estimated function that returns the probability. #Simula os dados nobs<-100 beta0=-10 beta1=0.006…
-
4
votes3
answers180
viewsA: How to include a variable high to n in regression
Another way to do regression is to use the Poly function x<-rnorm(100,1,10000) y<-rnorm(100,1,10000)+2*x+x^2 model1<-lm(y~poly(x,degree=2,raw=T))
-
4
votes3
answers180
viewsQ: How to include a variable high to n in regression
Suppose I have the following data x<-rnorm(100,1,10000) y<-rnorm(100,1,10000)+2*x+x^2 If I use the lm function as follows: model1<-lm(y~x+x^2) The R does not understand that it is to place…
-
2
votes2
answers195
viewsA: How to load all functions from a folder?
To load the functions can be used a for-loop where the functions you want to load are. setwd("D:") funcoes<-list.files() len<-length(funcoes) for (i in 1:len) {source(funcoes[i]) }…
-
2
votes2
answers195
viewsQ: How to load all functions from a folder?
Suppose the folder "D:" has several functions that I use in a project. How to load all these functions with a script?
-
8
votes3
answers17265
viewsA: How to sort a column-by-column data.frame in R?
You can also use the package plyr for the ordination. library(plyr) arrange(dados,desc(z)) w x y z 1 B C 1.5952808 0.7383247 2 A D -0.8356286 0.4874291 3 A D -0.6264538 0.3295078 4 B C 0.1836433…