Re: [R] Scatter plot with error bars

2013-06-27 Thread Blaser Nello
Are you sure, you want to calculate 68% confidence intervals? Use the add-argument in ?errorbar to add to the previous plot. errbar(x2,y2,y2+1.96*SD2, y2-1.96*SD2, col="green",pch=19, add=TRUE) Best, Nello -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-p

[R] Add constraints to rbinom

2013-06-27 Thread Anamika Chaudhuri
Hi: I am trying to generate Beta-Binomial random variables and then calculate Binomial exact confidence intervals to the rate..I was wondering if I needed to add a constraint such that x<=n to it, how do I go about it. The reason is I am getting data where x>n which is giving a rate>1. Heres my co

[R] Changing legend to fill colour in ggplot

2013-06-27 Thread Suparna Mitra
Hello R experts, I am having a problem to edit legend in ggplot using four variables. My data structure is : str(df) 'data.frame': 10 obs. of 6 variables: $ id: Factor w/ 2 levels "639A","640": 1 1 1 1 1 2 2 2 2 2 $ species : Factor w/ 5 levels "acine

Re: [R] How to create a function returning an array ?

2013-06-27 Thread David Winsemius
On Jun 27, 2013, at 8:04 PM, Kaptue Tchuente, Armel wrote: > Hi there, > > I would like to know how to change the line "img=as.single(rnorm(m)))" such > that instead of being a vector of length m as it is now, img is an array of > dimension c=(n,m,o) for instance > > -

[R] How to create a function returning an array ?

2013-06-27 Thread Kaptue Tchuente, Armel
Hi there, I would like to know how to change the line "img=as.single(rnorm(m)))" such that instead of being a vector of length m as it is now, img is an array of dimension c=(n,m,o) for instance - read_ts<-function(n,m,o,img) { out<-.Fortran("read_ts",

Re: [R] Comparing each level of a factor to the global mean

2013-06-27 Thread David Winsemius
On Jun 27, 2013, at 3:47 PM, Shaun Jackman wrote: > Hi Jean, > > contr.treatment(4) shows what the default contrast matrix looks like > for a factor with 4 levels. What function do I use to create a > contrast matrix to compare each level with the global mean (four > comparisons in total), and p

[R] Step wave Detection using R

2013-06-27 Thread Piyush Kumar
I am trying to detect a positively increasing trend in a time series data, to which when I apply the rolling mean it almost turns into a step wave function. The sharp rise in slope of rolling mean is an indication of positive trend, thus the rolling mean becomes like a step wave function. Is there

[R] Scatter plot with error bars

2013-06-27 Thread beginner
Hi I would like to plot multiple data sets on a scatter plot with error bars. To do this I write the following code: install.packages("Hmisc") library(Hmisc) x1<-data1[,1] y1<-data1[,2] x2<-data2[,1] y2<-data2[,2] x3<-data3[,1] y3<-data3[,2] SD1<-data1[,3] SD2<-data2[,3] SD3<-data3[,4] delta<-

Re: [R] Comparing each level of a factor to the global mean

2013-06-27 Thread Shaun Jackman
Hi Jean, contr.treatment(4) shows what the default contrast matrix looks like for a factor with 4 levels. What function do I use to create a contrast matrix to compare each level with the global mean (four comparisons in total), and produce a table similar to `summary.lm`? Thanks, Shaun On 26 J

Re: [R] Data Package Query

2013-06-27 Thread Rolf Turner
On 28/06/13 04:47, Jeff Newmiller wrote: A common error by beginners (which may or may not be your problem in this case) is to create a variable called "data". Unfortunately this hides the function named "data" and from that time forward that R session doesn't work when you type example c

Re: [R] matrix data generation under 2 loops

2013-06-27 Thread arun
Hi, May be this helps: res1<-as.matrix(transform(expand.grid(k1=0.3*(1:10),k2=1:10),k3=k1*k2)) A.K. Dear all, I'm trying to generate a matrix dataset following 2 loops analysis, such as the follows: for (i in 1:10)  { k1 <- 0.3 x i for (j in 1:10)  { k2 <- j k3 <- k1*k2 }}

Re: [R] multivariate version of aggregate

2013-06-27 Thread arun
Hi, May be this also helps: library(data.table) dt1<- data.table(dat) dt1[,cor(x,y),by=g] #   g  V1 #1: A -0.05643063 #2: B  0.16465040 dt1[,cor(x,y),by=g]$V1 #[1] -0.05643063  0.16465040 A.K. - Original Message - From: David Carlson To: 'Jannis' ; 'r-help' Cc: Sent: Thursday,

Re: [R] using "rollapply" to calculate a moving sum or running sum?

2013-06-27 Thread arun
Hi, Try: library(zoo) rollapply(t(mymatrix),width=12,FUN=sum,by.column=T,fill=NA,partial=FALSE,align="left")  #     [,1] [,2] [,3] [,4] [,5]  #[1,]  342  354  366  378  390  #[2,]  402  414  426  438  450  #[3,]  462  474  486  498  510  #[4,]  522  534  546  558  570  #[5,]  582  594  606  618  6

Re: [R] using "rollapply" to calculate a moving sum or running sum?

2013-06-27 Thread Gabor Grothendieck
On Thu, Jun 27, 2013 at 3:00 PM, Anika Masters wrote: > #using "rollapply" to calculate a moving sum or running sum? > > #I am tryign to use rollapply to calcualte a moving sum? #I tried > rollapply and get the error message > #"Error in seq.default(start.at, NROW(data), by = by) : > # wrong sign

Re: [R] using "rollapply" to calculate a moving sum or running sum?

2013-06-27 Thread Adams, Jean
This gets you close ... Jean library(zoo) t(apply(mymatrix, 1, rollapply, w, sum)) On Thu, Jun 27, 2013 at 2:00 PM, Anika Masters wrote: > #using "rollapply" to calculate a moving sum or running sum? > > #I am tryign to use rollapply to calcualte a moving sum? #I tried > rollapply and get the

Re: [R] creat raster from XYZ

2013-06-27 Thread Michael Sumner
On Fri, Jun 28, 2013 at 1:41 AM, Shane Carey wrote: > Hi Michael, > > I tried this but it would not work. I get an error stating that x cell > sizes are not regular. > Any ideas? > The function is reporting that your data don't fall on a grid, you'll need to figure out why that is the case, or i

Re: [R] [SPAM?] Re: Data Package Query

2013-06-27 Thread Jeff Newmiller
Please reply to all so the thread stays on the mailing list for all to see (and correct if wrong information is given). Regarding your code below, either you are missing a library statement to load a package that contains this "trial" data, or you are intending to read in your own data set but

Re: [R] Inhomogeneous, univariate, O-Ring Statistic

2013-06-27 Thread Ute Hahn
Hi J Ely, This would very much depend on how an O-ring statistic for inhomogeneous point patterns is defined. The formula in Wiegand and Moloney (2004), O(r)=g(r)*lambda, straightforwardly only makes sense for a homogeneous pattern, where the intensity lambda is a constant. Is there an "official"

Re: [R] when to use & pros/cons of dataframe vs. matrix?

2013-06-27 Thread Rui Barradas
Hello, Arun's answer shows that matrices are faster. If your data is all of the same type, then this might be a point for matrices. data.frames are better for modeling. You can use the formula interface to the many modeling functions. For instance, the example below is _not_ possible with a ma

Re: [R] Write an Excel workbook?

2013-06-27 Thread Marc Schwartz
On Jun 27, 2013, at 2:14 PM, Spencer Graves wrote: > Hello: > > > Is there a fully transportable way to write an Excel workbook? > > > The "writeFindFn2xls" function in the "sos" package attempts to write search > results to an Excel workbook consisting of (1) a summary of the packages >

Re: [R] multivariate version of aggregate

2013-06-27 Thread Rui Barradas
Hello, Or use ?sapply. sapply(split(dat[1:2], dat[3]), function(x) cor(x[1], x[2])) Hope this helps, Rui Barradas Em 27-06-2013 20:22, David Carlson escreveu: You can pass a matrix to by() set.seed(42) dat <- data.frame(x=runif(50)*20, y=runif(50)*20, g=rep(LETTERS[1:2], each=25)) as.ve

Re: [R] LSD comparison

2013-06-27 Thread arun
Hi,Without any reproducible example or code, it is difficult to understand the problem.  Did you load the package? Using the example in ?LSD.test() library(agricolae) data(sweetpotato)   model<-aov(yield~virus, data=sweetpotato)   out <- LSD.test(model,"virus", p.adj="bonferroni",   ma

Re: [R] multivariate version of aggregate

2013-06-27 Thread Greg Snow
Look at the plyr package, probably the ddply function in that package. You can write your own function to do whatever you want on the pieces of the split apart object. Correlation between a specified pair of columns would be simple. On Thu, Jun 27, 2013 at 11:26 AM, Jannis wrote: > Dear List

Re: [R] Write an Excel workbook?

2013-06-27 Thread andrija djurovic
XLConnect package Andrija On Jun 27, 2013 9:15 PM, "Spencer Graves" < spencer.gra...@structuremonitoring.com> wrote: > Hello: > > > Is there a fully transportable way to write an Excel workbook? > > > The "writeFindFn2xls" function in the "sos" package attempts to write > search results to an Exce

Re: [R] multivariate version of aggregate

2013-06-27 Thread David Carlson
You can pass a matrix to by() > set.seed(42) > dat <- data.frame(x=runif(50)*20, y=runif(50)*20, g=rep(LETTERS[1:2], each=25)) > as.vector(by(dat[,1:2], dat$g, function(x) cor(x)[1,2])) [1] -0.05643063 0.16465040 - David L Carlson Associate Professor of Anthro

[R] Write an Excel workbook?

2013-06-27 Thread Spencer Graves
Hello: Is there a fully transportable way to write an Excel workbook? The "writeFindFn2xls" function in the "sos" package attempts to write search results to an Excel workbook consisting of (1) a summary of the packages identified in the search, (2) the individual help pages found, and (3)

[R] using "rollapply" to calculate a moving sum or running sum?

2013-06-27 Thread Anika Masters
#using "rollapply" to calculate a moving sum or running sum? #I am tryign to use rollapply to calcualte a moving sum? #I tried rollapply and get the error message #"Error in seq.default(start.at, NROW(data), by = by) : # wrong sign in 'by' argument" #example: mymatrix <- ( matrix(data=1:100, nr

Re: [R] when to use & pros/cons of dataframe vs. matrix?

2013-06-27 Thread arun
Hi, set.seed(24) dat1<-data.frame(X=sample(letters,20,replace=TRUE),Y=sample(1:40,20,replace=TRUE),stringsAsFactors=FALSE) mat1<-as.matrix(dat1)  sapply(dat1,class) #  X   Y #"character"   "integer" sapply(split(mat1,col(mat1)),class) #  1   2 #"character" "chara

Re: [R] when to use & pros/cons of dataframe vs. matrix?

2013-06-27 Thread Don McKenzie
Anika -- these are good questions and many on the list could expatiate on them. These erudite people are also busy, however, and that is why the R-news posting guide suggests that you study an introductory book on R before asking general questions. On 27-Jun-13, at 11:26 AM, Anika Masters

[R] when to use & pros/cons of dataframe vs. matrix?

2013-06-27 Thread Anika Masters
When "should" I use a dataframe vs. a matrix? What are the pros and cons? If I have data of all the same type, am I usually better off using a matrix and not a dataframe? What are the advantages if any of using a dataframe vs. a matrix? (rownames and column names perhaps?) ___

Re: [R] Data Package Query

2013-06-27 Thread David Carlson
You can specify a package that exists, for example, datasets. You can use the data() function instead of the Data() function which does not exist. You could read one of the many, fine tutorials about R: http://cran.r-project.org/other-docs.html David -Original Message- From: Yasmine Refa

Re: [R] multiple csv files for T-test

2013-06-27 Thread arun
Hi, I used as.data.frame(matrix(...)) just to create an example dataset.  In your case, you don't need to do that.  Using the same example: set.seed(24) dat1<- as.data.frame(matrix(sample(20:40,40,replace=TRUE),ncol=4)) set.seed(285) dat2<- as.data.frame(matrix(sample(35:60,40,replace=TRUE),ncol=

Re: [R] "actuar" package query.

2013-06-27 Thread David Winsemius
On Jun 27, 2013, at 4:14 AM, Charles Thuo wrote: > I run the following in the "actuar" package while trying to discretize the > lognormal distribution which i had fitted using the "fitdistrplus" package. > > fx <- discretize(plnorm(11.69,2.1),from = 0, to = 22, step = 0.5, method = > "upper")

[R] multivariate version of aggregate

2013-06-27 Thread Jannis
Dear List members, i am seeking a multivariate version of aggregate. I want to compute, fro example the correlation between subsets of two vectors. In aggregate, i can only supply one vector with indices for subsets. Is there ready function for this or do i need to program my own? Cheers

Re: [R] Data Package Query

2013-06-27 Thread Jeff Newmiller
"data" is a base function (so you should not have to load any packages to use it), and it is not capitalized. Depending on the particular data set you want the data function to load, you may need to load the package that contains that data set. A common error by beginners (which may or may not

Re: [R] LSD comparison

2013-06-27 Thread Rui Barradas
Hello, After installing the package (just once) you have to load it in the R session (every time you start a session that will use it.) You do this with the following command. library(agricolae) # load the package Hope this helps, Rui Barradas Em 27-06-2013 11:17, bawonpon chonipat escreve

Re: [R] XYZ data

2013-06-27 Thread John Kane
You're right. I was in a hurry and misread the question John Kane Kingston ON Canada > -Original Message- > From: cl...@ecy.wa.gov > Sent: Wed, 26 Jun 2013 09:16:21 -0700 (PDT) > To: jrkrid...@inbox.com > Subject: Re: [R] XYZ data > > John, > > That still leaves a string of identical n

Re: [R] corrgram with two datasets

2013-06-27 Thread ilai
?pairs On Thu, Jun 27, 2013 at 2:48 AM, Meesters, Aesku.Kipp Institute < meest...@aesku-kipp.com> wrote: > Hi, > > I would like to display inter-parameter scatter plots like those with the > corrgram package (see upper triangle here: > http://www.statmethods.net/advgraphs/images/corrgram2.png ),

Re: [R] Data Package Query

2013-06-27 Thread David Carlson
You need to copy the lines you have typed in to R and send them exactly. The only way I get an error message like the one you indicate is with the following: > library(data) Error in library(data) : there is no package called 'data' > There is no Data function in R so if you typed that you would

Re: [R] Data Package Query

2013-06-27 Thread Richard M. Heiberger
you probably need to type data(datasetname) with a lowercase "d" for data(). R is case sensitive. On Thu, Jun 27, 2013 at 4:02 PM, Yasmine Refai wrote: > Hello, > > When i type in the below syntax: > Data (name of the data set) > > I get an error message specifying that the "data" package is

[R] combine attributes

2013-06-27 Thread Nico Bl
hello, i have to do a sna for a seminar. i have a csv.data with acteurs. i can identify a cooperation between acteurs with an ID in the csc.data. Is the ID equal with another acteur, so they have an cooperation. furthermore i have an information about the acteurs in the csv.data. there are thre

Re: [R] Lexical scoping is not what I expect

2013-06-27 Thread Hadley Wickham
> I too find R's lexical scoping rules straightforward. > However, I'd say that if your code relies on lexical scoping to find > something, you should probably rewrite your code. Except of course that almost every function relies on lexical scoping to some extent! Do you want: f <- function(a,

[R] LSD comparison

2013-06-27 Thread bawonpon chonipat
Dear Sir I'm a student in the university and just start to use R for agricultural research analysis. I know my question is an old question, but I don't find any clear explanation about my problem via internet. I'm sorry if my email is disturbing all of you. I install R with version 3.0.1 and

Re: [R] Fwd: Questions about working with a dataframe

2013-06-27 Thread Jacqueline Oehri
Dear Mr. Kane Thank you very much for your recommendations!!! And I apologise for all circumstances!! I will do so as you said in the future! Have a nice day, Best wishes Jacqueline   Gesendet: Mittwoch, 26. Juni 2013 um 17:34 Uhr Von: "John Kane" An: "Jacqueline Oehri" , "David Winsemius" ,

[R] are there any package for clustering the microarray data?

2013-06-27 Thread Zhaoran Zhou
Hello, i just begin to study R language. And now i need to do clustering analysis of some data from microarray experiments. are there any good package for doing that? If i get the clustering results, how can i get a matrix or list of all the members of a certain cluster? thanks! zhaoran

[R] party ctree : getting a one-node tree

2013-06-27 Thread Yuliya Matveyeva
Dear useRs, I am currently using the ctree function (package "party") and am stuck at the problem of getting a one-node tree (a tree with no splits consisting of the root only) even if I maximally loosen all the stopping criteria (mincriterion = 0, minbucket = 0, minsplit = 0). In order to chec

[R] corrgram with two datasets

2013-06-27 Thread Meesters, Aesku.Kipp Institute
Hi, I would like to display inter-parameter scatter plots like those with the corrgram package (see upper triangle here: http://www.statmethods.net/advgraphs/images/corrgram2.png ), just that I would like to plot two datasets instead of one. Say one with black and one with red dots. Or a merge

Re: [R] Change font size in Cluster analysis

2013-06-27 Thread Meesters, Aesku.Kipp Institute
Gitte, in addition to Brian Ripley's answer: Will you be able to read all those labels anyway? And: Have you considered using heatmap.2 (gplots package)? It's a little denser than your approach and lets you in control of the clustering function (defaults are the same as in your example). You wou

[R] "actuar" package query.

2013-06-27 Thread Charles Thuo
I run the following in the "actuar" package while trying to discretize the lognormal distribution which i had fitted using the "fitdistrplus" package. fx <- discretize(plnorm(11.69,2.1),from = 0, to = 22, step = 0.5, method = "upper") Error in discretize(plnorm(11.69, 2.1), from = 0, to = 22, st

[R] Data Package Query

2013-06-27 Thread Yasmine Refai
Hello, When i type in the below syntax: Data (name of the data set) I get an error message specifying that the "data" package is not found. Please note that i installed all packages having the word "data" included in them and loaded all these packages. Please advice. Regards! ___

[R] choicemodelr is misbehaving under R3.0

2013-06-27 Thread Dimitri Liakhovitski
Hi John (and other authors of ChoiceModelR package), I am experiencing a weird thing when using the function choicemodelr under R3.0.1. Before I updated to R3.0, I had used choicemodelr unde R2.15. It was always as fast or faster than exactly the same task in Sawtooth software. Now, I've tried to

Re: [R] multiple csv files for T-test

2013-06-27 Thread arun
Hi, May be this helps: #You can use ?read.csv() to read the two files. set.seed(24) dat1<- as.data.frame(matrix(sample(20:40,40,replace=TRUE),ncol=4)) set.seed(285) dat2<- as.data.frame(matrix(sample(35:60,40,replace=TRUE),ncol=4)) sapply(colnames(dat1),function(i) t.test(dat1[,i],dat2[,i],paired=

Re: [R] R CMD BATCH Unicode

2013-06-27 Thread Duncan Murdoch
On 27/06/2013 9:11 AM, Ned Harding wrote: The problem isn't so much getting the output to work. I can get that to work too. The problem is that R doesn't know what the encoding is, so things like graphs fail. I think the solution is pretty clear: don't expect R CMD BATCH to accept non-nat

Re: [R] R CMD BATCH Unicode

2013-06-27 Thread Ned Harding
The problem isn't so much getting the output to work. I can get that to work too. The problem is that R doesn't know what the encoding is, so things like graphs fail. -Original Message- From: Jeff Newmiller [mailto:jdnew...@dcn.davis.ca.us] Sent: Thursday, June 27, 2013 12:45 AM To: N

Re: [R] Combining levels

2013-06-27 Thread arun
Hi, May be this helps: Data1<-read.table(text=' TYPE    VALUE "Residential-future"    2.2 "Open space-managed"    1.4 "Mixed use"   5.2 "Residential-existing"  

Re: [R] Lexical scoping is not what I expect

2013-06-27 Thread Terry Therneau
Duncan, "I disagree with Duncan" was not at all the intent of my note, but on rereading it does have that flavor. Chastisement accepted. Due to the documentation angle I'd simply change your original "maybe" to "sometimes maybe". A bit more caution but the same message. Terry T. On 06/27/2

Re: [R] censor=FALSE and id options in survfit.coxph

2013-06-27 Thread Terry Therneau
Yes, it is a bug. Thanks for providing a complete example. I'll look into it, but leave for a week's vacation in a couple of hours and have some other pressing tasks. Terry T. Terry, I recently noticed the censor argument of survfit. For some analyses it greatly reduces the size of the re

Re: [R] Lexical scoping is not what I expect

2013-06-27 Thread Duncan Murdoch
On 13-06-27 8:18 AM, Terry Therneau wrote: I second Ellison sentiments of "almost never". One main reason is readability on later viewing. Yes, as Duncan says global variables can sometimes be handy and make functions quick to write, but using a formal argument in the call is always clearer.

Re: [R] Lexical scoping is not what I expect

2013-06-27 Thread Terry Therneau
I second Ellison sentiments of "almost never". One main reason is readability on later viewing. Yes, as Duncan says global variables can sometimes be handy and make functions quick to write, but using a formal argument in the call is always clearer. Terry Therneau On 06/27/2013 05:00 AM, r-he

Re: [R] creat raster from XYZ

2013-06-27 Thread Michael Sumner
On Wed, Jun 26, 2013 at 12:53 AM, Shane Carey wrote: > Hi, > > I have a data set consisting of XYZ data. the dimensions are 22427 rows by > 3 columns. > > I try to use rasterFromXYZ from the raster package but I get the following > error: > > Error in rasterFromXYZ(DATA) : x cell sizes are not re

[R] Combining levels

2013-06-27 Thread Shane Carey
Hi, I am trying to combine two levels and leave all other levels unchnaged. I have tried doing the following: combine_factor(DATA$Land_zone, c("mix use","Mixed use")) but it just returns NA. Thanks -- Shane [[alternative HTML version deleted]] ___

Re: [R] Change font size in Cluster analysis

2013-06-27 Thread Prof Brian Ripley
Your example is not reproducible (see the posting guide), but using example(hclust) hc <- hclust(dist(USArrests), "ave") plot(hc) plot(hc, cex = 0.3) the second has much smaller case names. On 27/06/2013 11:12, Gitte Brinch Andersen wrote: Hello I have done a cluster analysis, and I would li

Re: [R] help with plotmeans (gplots)

2013-06-27 Thread Simone Gabbriellini
Hi David, your code works perfectly on my dataset, thanks for the detailed example and for your time! Best, Simone 2013/6/26 David Carlson > Your data is probably not arranged correctly. See if this works for > your data: > > > # Creating a reproducible example > > set.seed(42) > > dat <- mat

[R] Change font size in Cluster analysis

2013-06-27 Thread Gitte Brinch Andersen
Hello I have done a cluster analysis, and I would like to change the font size of my sample names. I have tried using cex, but this doesn't work/change anything. I have tried to specify it with cex.main, cex.lab, cex.axis and cex.sub, and can change all other names in the picture but my sample

Re: [R] filling list of data frames

2013-06-27 Thread Jim Holtman
You might want to use Rprof to profile your code to understand where the time is going; it might be in the function you are callingband therefore the "for" loop might not be the issue. Sent from my iPad On Jun 27, 2013, at 4:19, "Frederico Mestre" wrote: > Hello: > > > > I have a list of d

Re: [R] create index for tapply or by

2013-06-27 Thread Gerrit Eichner
Hi, Witold, take a look at ?findIntervals It might give want you need. Hth -- Gerrit On Thu, 27 Jun 2013, Witold E Wolski wrote: Is there a build in function to create an index for tapply or by given a a numeric vector x an a vector of breaks? What I want to do is: x <- 1:100 breaks <-

Re: [R] create index for tapply or by

2013-06-27 Thread Rui Barradas
Hello, Try using ?cut y2 <- cut(x, breaks) by(x, y2, sum) Hope this helps, Rui Barradas Em 27-06-2013 09:24, Witold E Wolski escreveu: Is there a build in function to create an index for tapply or by given a a numeric vector x an a vector of breaks? What I want to do is: x <- 1:100 break

[R] create index for tapply or by

2013-06-27 Thread Witold E Wolski
Is there a build in function to create an index for tapply or by given a a numeric vector x an a vector of breaks? What I want to do is: x <- 1:100 breaks <- c(0,10,20,50,99,110) y <- rep(0,length(x)) for(i in 2:length(breaks)){ y[which(x>breaks[i-1] & x <= breaks[i])] <- i } by(x,y,sum) but

[R] filling list of data frames

2013-06-27 Thread Frederico Mestre
Hello: I have a list of data frames, built like this: the second df is a result of a function applied to the first, and so on. So the ith df is always dependent on the (i-1)th df. I've been doing this using for loops. However I think I have too many for loops which is making my code run slo