
12 Nov
2015
12 Nov
'15
18:02
Para mim, eu tive que fazer um ajuste um pouco diferente mas vou testar o que você fez também.Muuito obrigada! Estou mandando o que eu fiz abaixo: > AA<-read.table("/Users/bau/Dropbox/AA.N_ExemploReduzido.csv",header=TRUE + ,dec=".",as.is=TRUE, sep=",",, col.names=c("Data","hora", "bid","bidQ","ask","askQ","Preco","volume")) > library(dplyr) > a<-AA %>% group_by(Data) %>% summarise(maximo = max(Preco), minimo = min(Preco)) > a Source: local data frame [3 x 3] Data maximo minimo (int) (dbl) (dbl) 1 37988 38.06 38.00 2 37991 72.23 32.23 3 37992 41.45 38.90 > > > b<-AA %>% + group_by(Data) %>% + slice(which.min(hora)) %>% + ungroup %>% + group_by(Data) %>% + select(Primeiro = Preco) > b Source: local data frame [3 x 2] Groups: Data [3] Data Primeiro (int) (dbl) 1 37988 38.0 2 37991 37.9 3 37992 38.9 > > > > c<-AA %>% + group_by(Data) %>% + slice(which.max(hora)) %>% + ungroup %>% + group_by(Data) %>% + select(Ultimo = Preco) > > c Source: local data frame [3 x 2] Groups: Data [3] Data Ultimo (int) (dbl) 1 37988 38.05 2 37991 37.90 3 37992 38.90 > > > d<-cbind(a,b[,2],c[,2]) > d Data maximo minimo Primeiro Ultimo 1 37988 38.06 38.00 38.0 38.05 2 37991 72.23 32.23 37.9 37.90 3 37992 41.45 38.90 38.9 38.90 Em 12 de novembro de 2015 15:01, Thiago Serafim <thiago.serafim@gmail.com> escreveu: > Acabei de atualizar o R e o pacote dplyr. > > > sessionInfo() > R version 3.2.2 (2015-08-14) > Platform: x86_64-w64-mingw32/x64 (64-bit) > Running under: Windows 8 x64 (build 9200) > > locale: > [1] LC_COLLATE=Portuguese_Brazil.1252 LC_CTYPE=Portuguese_Brazil.1252 > LC_MONETARY=Portuguese_Brazil.1252 > [4] LC_NUMERIC=C LC_TIME=Portuguese_Brazil.1252 > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > other attached packages: > [1] dplyr_0.4.3 > > loaded via a namespace (and not attached): > [1] magrittr_1.5 R6_2.1.1 assertthat_0.1 parallel_3.2.2 DBI_0.3.1 > tools_3.2.2 Rcpp_0.12.1 > > Nessa versão do R e do dplyr, os seguintes comandos funcionam pra mim(um > pouco diferente da minha primeira sugestão) > > AA %>% group_by(Data) %>% filter(Hora == min(Hora)) %>% select(Primeiro > = Preco) > Source: local data frame [3 x 2] > Groups: Data [3] > > Data Primeiro > (int) (dbl) > 1 37988 38.0 > 2 37991 37.9 > 3 37992 38.9 > > AA %>% group_by(Data) %>% filter(Hora == max(Hora)) %>% select(Ultimo = > Preco) > Source: local data frame [3 x 2] > Groups: Data [3] > > Data Ultimo > (int) (dbl) > 1 37988 38.05 > 2 37991 37.90 > 3 37992 38.90 > > > > 2015-11-11 23:44 GMT-02:00 Mauro Sznelwar <sznelwar@uol.com.br>: > >> Porque quando rodo não dá certo? Eu coloquei os dois bancos de dados AA e >> DATA >> >> AA %>% group_by(Data) %>% summarise(maximo = max(Preco), minimo = >> min(Preco)) >> >> Error: index out of bounds >> >> >> >> > library(dplyr) >> >> > AA %>% group_by(Data) %>% summarise(maximo = max(Preco), minimo = >> min(Preco)) >> >> Source: local data frame [3 x 3] >> >> >> >> Data maximo minimo >> >> 1 37988 38.06 38.0 >> >> 2 37991 37.90 37.9 >> >> 3 37992 38.90 38.9 >> >> >> >> > AA %>% group_by(Data) %>% slice(which.min(Hora)) %>% select(Primeiro = >> Preco) >> >> Source: local data frame [3 x 2] >> >> Groups: Data >> >> >> >> Data Primeiro >> >> 1 37988 38.0 >> >> 2 37991 37.9 >> >> 3 37992 38.9 >> >> >> >> AA %>% group_by(Data) %>% slice(which.max(Hora)) %>% select(Ultimo = >> Preco) >> >> Source: local data frame [3 x 2] >> >> Groups: Data >> >> >> >> Data Ultimo >> >> 1 37988 38.05 >> >> 2 37991 37.90 >> >> 3 37992 38.90 >> >> >> >> >> >> 2015-11-11 14:28 GMT-02:00 salah <salah3.1416@gmail.com>: >> >> Olá, segue uma sugestão >> >> DADOS = structure(list(Data = c(37988L, 37988L, 37988L, 37988L, 37988L, >> 37988L, 37991L, 37991L, >> 37991L, 37991L, 37992L, 37992L, 37992L, >> 37992L), Hora = >> c(34251.038, 34263.36, 34296.73, 34301.369, >> 34318.91, 34324.707, >> 34398.295, 34400.585, 34416.511, 34425.892, >> 34250.443, 34252.127, >> 34266.757, 34273.928), X1 = c(-1, -1, >> 38.05, 38.06, 38.06, >> 38.04, 37.8, 37.8, 37.85, 37.85, -1, -1, >> 38.85, 38.85), X2 = >> c(-1L, -1L, 32L, 2L, 4L, 1L, 50L, 50L, >> 1L, 1L, -1L, -1L, 1L, >> 1L), X3 = c(-1, -1, 38.08, 38.08, 38.07, >> 38.07, 38.3, 38.3, 37.95, >> 37.95, -1, -1, 38.95, 38.95), X4 = >> c(-1L, >> -1L, 1L, 1L, 1L, 1L, >> 203L, 203L, 1L, 1L, -1L, -1L, 1L, 1L), Preco >> = c(38, >> 38, 38.05, 38.06, 38.06, >> 38.05, 37.9, 37.9, 37.9, 37.9, 38.9, >> 38.9, 38.9, 38.9), Volume >> = c(600L, 5000L, 6000L, 1500L, 500L, >> 4500L, 161800L, 500L, >> 3700L, 1100L, 125000L, 1100L, 400L, 1000L >> )), .Names = c("Data", "Hora", "X1", >> "X2", "X3", >> "X4", "Preco", "Volume"), class = >> "data.frame", row.names = c(NA, >> -14L)) >> library(data.table) >> >> ## converte data.frame para data.table >> setDT(DADOS) >> ## em list voce pode escolher max, min, mean, sd, etc. Em by voce >> escolhe por preco >> ## por Data, qual variavel que deseja >> >> ## por volume e data >> DADOS[, list(Precomax=max(Preco), Precomin=min(Preco)), by = .(Data, >> Volume)] >> >> ## por data >> DADOS[, list(Precomax=max(Preco), Precomin=min(Preco)), by = .(Data)] >> >> ?data.table >> >> >> >> Em Qua, 2015-11-11 às 13:00 -0200, Michelle Bau Graczyk escreveu: >> > Caros, bom dia… >> > >> > >> > Estou com uma dúvida bem boba mas não consigo resolver! >> > >> > >> > Eu tenho um data frame que a primeira coluna é a data, a segunda é o >> > horário e as outras seis são dados como preço e volume. >> > Os dias estão em forma numérica e eu os deixei assim porque achei que >> > seria mais fácil fazer o laço. O objetivo é que para cada daia eu ache >> > o valor inicial e final do preço assim como o seu máximo e seu mínimo. >> > Estou tendo problemas somente em trocar de dia. >> > Abaixo segue um pedaço reduzido do data frame e o código. >> > Alguém poderia me dar uma dica? >> > Muito obrigada! >> > AA<-read.table("/Users/bau/Dropbox/AA.N_ExemploReduzido.csv",header=TRUE >> > + ,dec=".",as.is=TRUE, sep=",") >> > > #AA >> > > dput(AA) >> > structure(list(X37988 = c(37988L, 37988L, 37988L, 37988L, 37988L, >> > 37988L, 37991L, 37991L, 37991L, 37991L, 37992L, 37992L, 37992L, >> > 37992L), X34244.059 = c(34251.038, 34263.36, 34296.73, 34301.369, >> > 34318.91, 34324.707, 34398.295, 34400.585, 34416.511, 34425.892, >> > 34250.443, 34252.127, 34266.757, 34273.928), X.1 = c(-1, -1, >> > 38.05, 38.06, 38.06, 38.04, 37.8, 37.8, 37.85, 37.85, -1, -1, >> > 38.85, 38.85), X.1.1 = c(-1L, -1L, 32L, 2L, 4L, 1L, 50L, 50L, >> > 1L, 1L, -1L, -1L, 1L, 1L), X.1.2 = c(-1, -1, 38.08, 38.08, 38.07, >> > 38.07, 38.3, 38.3, 37.95, 37.95, -1, -1, 38.95, 38.95), X.1.3 = >> > c(-1L, >> > -1L, 1L, 1L, 1L, 1L, 203L, 203L, 1L, 1L, -1L, -1L, 1L, 1L), X38 = >> > c(38, >> > 38, 38.05, 38.06, 38.06, 38.05, 37.9, 37.9, 37.9, 37.9, 38.9, >> > 38.9, 38.9, 38.9), X55900 = c(600L, 5000L, 6000L, 1500L, 500L, >> > 4500L, 161800L, 500L, 3700L, 1100L, 125000L, 1100L, 400L, 1000L >> > )), .Names = c("X37988", "X34244.059", "X.1", "X.1.1", "X.1.2", >> > "X.1.3", "X38", "X55900"), class = "data.frame", row.names = c(NA, >> > -14L)) >> > > >> > > matriz<-matrix(NA,230,4)## numero de linhas é o numero de dias. >> > > colnames(matriz)<-c("open","close","low","higth") >> > > i<-1 >> > > for( x in 37988:38000){ >> > + >> > + >> > + #x<-37988 >> > + >> > + if(AA$data==x){ >> > + AA1<-subset(AA,AA$data==x) >> > + matriz[i,1]<-AA1[1,7] >> > + matriz[i,2]<-AA1[nrow(AA1),7] >> > + matriz[i,3]<-min(AA1$last) >> > + matriz[i,4]<-max(AA1$last) >> > + >> > + i=i+1 >> > + } >> > + >> > AA<-read.table("/Users/bau/Dropbox/AA.N_ExemploReduzido.csv",header=TRUE >> > + ,dec=".",as.is=TRUE, sep=",") >> > + #AA >> > + dput(AA) >> > + >> > + matriz<-matrix(NA,3,4)## numero de linhas é o numero de dias. >> > + colnames(matriz)<-c("open","close","low","higth") >> > + i<-1 >> > + for( x in 37988:37992){ >> > + >> > + >> > + >> > + >> > + if(AA$data==x){ >> > + AA1<-subset(AA,AA$data==x) >> > + matriz[i,1]<-AA1[1,7] >> > + matriz[i,2]<-AA1[nrow(AA1),7] >> > + matriz[i,3]<-min(AA1$last) >> > + matriz[i,4]<-max(AA1$last) >> > + >> > + i=i+1 >> > + } >> > + } >> > + matriz >> > + write.table(matriz,"Matriz4ParametrosPreço.txt") >> > + >> >> > _______________________________________________ >> > R-br mailing list >> > R-br@listas.c3sl.ufpr.br >> > https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br >> > Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e fornea >> cdigo mnimo reproduzvel. >> >> >> _______________________________________________ >> R-br mailing list >> R-br@listas.c3sl.ufpr.br >> https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br >> Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e fornea >> cdigo mnimo reproduzvel. >> >> >> >> >> ------------------------------ >> [image: Avast logo] <https://www.avast.com/antivirus> >> >> Este email foi escaneado pelo Avast antivírus. >> www.avast.com <https://www.avast.com/antivirus> >> >> >> _______________________________________________ >> R-br mailing list >> R-br@listas.c3sl.ufpr.br >> https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br >> Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e forneça >> código mínimo reproduzível. >> > > > _______________________________________________ > R-br mailing list > R-br@listas.c3sl.ufpr.br > https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br > Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e forneça > código mínimo reproduzível. >