[R] 7 days confusion over lists

R. Michael Weylandt michael.weylandt at gmail.com
Thu Jul 5 17:33:12 CEST 2012


Could you make a reproducible example?
[http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example]
I can't run any of your code as is. A few comments inline.

On Thu, Jul 5, 2012 at 9:44 AM, Wageningen-eR <igor.milosavljevic at wur.nl> wrote:
> Hello,
>
> I am a Masters student and I am working on my thesis modelling smallholder
> farms using a program in R.  I have modified the original code and I am
> having some issues with lists that I cannot figure out.
>
> Originally, I had list file defining lists such as: Param, Crop1, Crop1,
> Soil, etc.  (ex. Param <- list() ).  Their subsets were listed as
> Crop1$CContent for example and there was quite a few of them.  There is a
> run file that gets the list file going in the following code:
>
> #--Initialising the lists to store variable values
> tmp <- init_lists()
> Param       <- tmp[[1]]
> Crop1       <- tmp[[2]]
> Fert        <- tmp[[3]]
> Meteo       <- tmp[[4]]
> Soil        <- tmp[[5]]
> RainPattern <- tmp[[6]]
> Crop2       <- tmp[[7]]
> Cropres     <- tmp[[8]]
> rm(tmp)
>
> The problem here is that the lists get confused with each other; that is
> elements of Soil appear in Crop2.  In the run file, I changed the order of
> the lists (for example Fert <- tmp[[5]] and Crop2 <-tmp[[3]]) and it changes
> which ones are confused with what; with some lists not being confused at
> all.  I cannot find why this is a problem as the tmps are removed at the end
> of the command list shown below and each element is clearly defined as a
> part of its given list with (list name)$(Sublist maybe)$(specific element
> name)
>
> Either way, I changed the original lists to be more specific, for example:
>
> Crop$ <- list(
> CContent <- NA,
> NContent <- NA,
> PContent <- NA,
> KContent <- NA,
> K <- list(
> uptakegivenN,
> uptakegivenP),
> dryMatterContent
> ...etc.
>

This almost certainly doesn't work. "Crop$ <- ... " should be a syntax error.

> The issue then became that it stops at a certain point accepting the
> assigned values and keeps them as null.  That is, the list contains an
> element that states Crop2$minYieldN <- NA but the value becomes NULL when it
> is run.   Also, the list, say Param, contains both:
>
> [[1]]
> [1] NA
>
> [[2]]
> [1] NA
>
> [[3]]
> [[3]][[1]]
> [1] NA
>
> [[3]][[2]]
> [1] NA
>
> and some of the value names (up to a point)
>
> $inertCRTR
> [1] 0.001
>
> $humificationFactor
> [1] 0.25
>
> $fractionStabilisedSOMC
> [1] 0.2
>
> $growthEffectMicroorganisms
> [1] 0.6
>
> Is it perhaps that the list is too long?  Please let me know if anything
> comes to mind, it would be great help.
>

No, it's not a length thing. It might be a partial matching thing though.

x <- list()

x$a <- 5

x$ab <- 10

x$ba <- 15

print(x$b) # where's x$b?

It's recommended to use the double `[[` in programming applications to
avoid this trouble.

Best,
Michael

> Thank you!
>
>
> Regards,
>
> -Igor Milosavljevic
> Wageningen University and Research Center
>
> --
> View this message in context: http://r.789695.n4.nabble.com/7-days-confusion-over-lists-tp4635489.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.



More information about the R-help mailing list