[R] Error in for-loop

jim holtman jholtman at gmail.com
Sun Jul 29 18:43:37 CEST 2012


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.



More information about the R-help mailing list