[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?
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:
> 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[]
> Crop1 <- tmp[]
> Fert <- tmp[]
> Meteo <- tmp[]
> Soil <- tmp[]
> RainPattern <- tmp[]
> Crop2 <- tmp[]
> Cropres <- 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[] and Crop2 <-tmp[]) 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
> 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(
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:
>  NA
>  NA
>  NA
>  NA
> and some of the value names (up to a point)
>  0.001
>  0.25
>  0.2
>  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.
> Thank you!
> -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
> 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