Hello erverybody, I have a problem with my second for-loop. 1. First i read the tables. datos.mx1 <- read.table('PETmx1.csv',head=TRUE,sep=';') datos.min <- read.table('PETmin.csv',head=TRUE,sep=';') http://r.789695.n4.nabble.com/file/n4638257/PETmx1.csv PETmx1.csv http://r.789695.n4.nabble.com/file/n4638257/PETmin.csv PETmin.csv names(datos.mx1) "Fecha" "Mes" "Serra.da.Foladoira" "Santiago" "Sergude" "Rio.Do.Sol" You find in the first column the date and in the second only the month. The other columns contain the temperature of the respective weather stations. 2. My aim is to calculate thresholds for each weather station. superior <- NULL inferior <- NULL LC <- NULL LF <- NULL for (i in 3:6){ superior[i] <- 23+(datos.mx1[i]-23)*0.33; inferior[i] <- 18+(datos.min[i]-18)*0.33; } #Until here everything is ok, but the next loop issues an error. I must multiply the results of each weather station with a constant. for (i in 3:6){ LF[i] <- 1.26086956521739*superior[[i]]; LC[i] <- 0.722222222222222*inferior[[i]] } The error is: "Number of items to replace is not a multiple of replacement length". I hope someone can help me. I thank you very much. Best regards, Domi -- View this message in context: http://r.789695.n4.nabble.com/Error-in-for-loop-tp4638257.html Sent from the R help mailing list archive at Nabble.com.
Try this: You had some 'indexing' problems. You were accessing the columns of the dataframe incorrectly. You are also accessing 'list' so you might want to review the Intro to R on indexing:> datos.mx1 <- read.table('http://r.789695.n4.nabble.com/file/n4638257/PETmx1.csv',head=TRUE,sep=';') > datos.min <- read.table('http://r.789695.n4.nabble.com/file/n4638257/PETmin.csv',head=TRUE,sep=';')> > superior <- NULL > inferior <- NULL > LC <- NULL > LF <- NULL > > > for (i in 3:6){+ superior[[i]] <- 23+(datos.mx1[[i]]-23)*0.33; + inferior[[i]] <- 18+(datos.min[[i]]-18)*0.33; + }> > #Until here everything is ok, but the next loop issues an error. I must> #multiply the results of each weather station with a constant.> > for (i in 3:6){+ + LF[[i]] <- 1.26086956521739*superior[[i]] + LC[[i]] <- 0.722222222222222*inferior[[i]] + }> str(LF)List of 6 $ : NULL $ : NULL $ : num [1:1826] 21.1 21.7 23.5 23.5 23.7 ... $ : num [1:1826] 25.4 23.5 24.8 25 24.5 ... $ : num [1:1826] 23.3 24 24.7 24.7 24.9 ... $ : num [1:1826] 21.8 22.6 23.5 23.5 22.8 ...> str(LC)List of 6 $ : NULL $ : NULL $ : num [1:1826] 7.83 8.5 8.21 7.83 8.28 ... $ : num [1:1826] 9.07 9.76 8.54 8.07 9.35 ... $ : num [1:1826] 8.66 8.95 8.61 7.61 9.12 ... $ : num [1:1826] 8.07 8.69 8.07 7.85 8.23 ... On Sun, Jul 29, 2012 at 9:59 AM, Domi <dominic.roye at gmail.com> wrote:> Hello erverybody, > > I have a problem with my second for-loop. > > 1. First i read the tables. > > datos.mx1 <- read.table('PETmx1.csv',head=TRUE,sep=';') > datos.min <- read.table('PETmin.csv',head=TRUE,sep=';') > > http://r.789695.n4.nabble.com/file/n4638257/PETmx1.csv PETmx1.csv > http://r.789695.n4.nabble.com/file/n4638257/PETmin.csv PETmin.csv > > names(datos.mx1) > > "Fecha" "Mes" "Serra.da.Foladoira" > "Santiago" "Sergude" "Rio.Do.Sol" > > You find in the first column the date and in the second only the month. The > other columns contain the temperature of the respective weather stations. > > 2. My aim is to calculate thresholds for each weather station. > > superior <- NULL > inferior <- NULL > LC <- NULL > LF <- NULL > > > for (i in 3:6){ > superior[i] <- 23+(datos.mx1[i]-23)*0.33; > inferior[i] <- 18+(datos.min[i]-18)*0.33; > } > > #Until here everything is ok, but the next loop issues an error. I must > multiply the results of each weather station with a constant. > > for (i in 3:6){ > > LF[i] <- 1.26086956521739*superior[[i]]; > LC[i] <- 0.722222222222222*inferior[[i]] > } > > > The error is: "Number of items to replace is not a multiple of replacement > length". > > > I hope someone can help me. I thank you very much. > > Best regards, > > Domi > > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Error-in-for-loop-tp4638257.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
Domi wrote on 08/01/2012 Jul 29, 2012; 8:59am:> > Hello erverybody, > > I have a problem with my second for-loop. > > 1. First i read the tables. > > datos.mx1 <- read.table('PETmx1.csv',head=TRUE,sep=';') > datos.min <- read.table('PETmin.csv',head=TRUE,sep=';')You are most likely to get good help with your R problem if you provide code that allows someone else to recreate your problem by just copying and pasting R code. You could use dput() to share the first 10 rows of your two data files, instead of attaching your csv files. datos.mx1 <- structure(list(Fecha = c("01.01.2006", "02.01.2006", "03.01.2006", "04.01.2006", "05.01.2006", "06.01.2006", "07.01.2006", "08.01.2006", "09.01.2006", "10.01.2006"), Mes = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Serra.da.Foladoira = c(4.1, 5.5, 9.9, 9.7, 10.2, 4.2, 1, 9.4, 8.3, 11.8), Santiago = c(14.4, 9.8, 13, 13.3, 12.2, 14.2, 6.9, 12.2, 8.7, 12.9), Sergude = c(9.3, 11, 12.7, 12.7, 13.1, 10.2, 3.2, 11.7, 9.1, 8.6), Rio.Do.Sol = c(5.7, 7.6, 9.9, 9.7, 8, 5.4, 3.2, 10.9, 8.2, 9.8)), .Names = c("Fecha", "Mes", "Serra.da.Foladoira", "Santiago", "Sergude", "Rio.Do.Sol"), row.names = c(NA, 10L), class = "data.frame") datos.min <- structure(list(Fecha = c("01.01.2006", "02.01.2006", "03.01.2006", "04.01.2006", "05.01.2006", "06.01.2006", "07.01.2006", "08.01.2006", "09.01.2006", "10.01.2006"), Mes = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Serra.da.Foladoira = c(-3.7, -0.9, -2.1, -3.7, -1.8, -4, -5.2, -3.9, -4.4, -5), Santiago = c(1.5, 4.4, -0.7, -2.7, 2.7, 0.4, -1.3, -4, -2.2, -6.7), Sergude = c(-0.2, 1, -0.4, -4.6, 1.7, -1.4, -3.3, -4.5, -6.4, -8.3), Rio.Do.Sol = c(-2.7, -0.1, -2.7, -3.6, -2, -3.5, -5.4, -4.2, -5.2, -5.5)), .Names = c("Fecha", "Mes", "Serra.da.Foladoira", "Santiago", "Sergude", "Rio.Do.Sol" ), row.names = c(NA, 10L), class = "data.frame")> PETmx1.csvPETmin.csv > > names(datos.mx1) > > "Fecha" "Mes" "Serra.da.Foladoira" > "Santiago" "Sergude" "Rio.Do.Sol" > > You find in the first column the date and in the second only the > month. The other columns contain the temperature of the respective > weather stations. > > 2. My aim is to calculate thresholds for each weather station. > > superior <- NULL > inferior <- NULL > LC <- NULL > LF <- NULL > > > for (i in 3:6){ > superior[i] <- 23+(datos.mx1[i]-23)*0.33; > inferior[i] <- 18+(datos.min[i]-18)*0.33; > }# You don't need a loop to execute your first step superior <- 23 + (datos.mx1[, 3:6] - 23)*0.33 inferior <- 18 + (datos.min[, 3:6] - 18)*0.33> #Until here everything is ok, but the next loop issues an error. I > must multiply the results of each weather station with a constant. > > for (i in 3:6){ > > LF[i] <- 1.26086956521739*superior[[i]]; > LC[i] <- 0.722222222222222*inferior[[i]] > }# You don't need a loop to execute your second step either LF <- 1.26086956521739*superior LC <- 0.722222222222222*inferior> The error is: "Number of items to replace is not a multiple of > replacement length". > > > I hope someone can help me. I thank you very much. > > Best regards, > > Domi[[alternative HTML version deleted]]