How to create a loop in R for iteratively building array elements?

I am trying to create a loop to extract data from an array created earlier, so I can use the extracted data to generate a line graph.

So far, I have done this manually using:

allweek1<-(data.frame(t_weekmean[,,1])) #which selects the date and generates the data frame I want to later format the date using week1<-stack(allweek1) #and then plot it using plot(week1$values,type="n", xlim=c(0,2),xlab="Weight (gr)",ylab="Rate (umol/L*gr)",main="All individuals and Treatments at all times") lines(week1$values[week1$ind=="X9"]~x,type="o",col="red") lines(week1$values[week1$ind=="X12"]~x,type="o",col="blue") lines(week1$values[week1$ind=="X15"]~x,type="o",col="green") lines(week1$values[week1$ind=="X18"]~x,type="o",col="purple"). 

I know that there must be a way to do this in a loop, for this example I give only two weeks, but my data increases to 30, and doing it manually will be erratic and easy to have errors.

This is the initial array that I have:

 , , Week = 1 Temp variable 9 12 15 18 X0 100.000 100.000 100.000 100.000 X0.5 98.855 98.591 98.357 99.003 X1 98.004 97.804 97.638 98.299 X1.5 95.953 96.999 96.810 97.555 X2 95.235 96.078 95.346 96.665 , , Week = 2 Temp variable 9 12 15 18 X0 100.000 100.000 100.000 100.000 X0.5 99.137 99.035 97.883 99.055 X1 98.420 98.298 96.459 97.765 X1.5 97.939 97.181 94.406 96.546 X2 96.998 96.237 91.906 95.263 

The following data frame, which is then converted to the stack version:

  X9 X12 X15 X18 X0 100.000 100.000 100.000 100.000 X0.5 98.855 98.591 98.357 99.003 X1 98.004 97.804 97.638 98.299 X1.5 95.953 96.999 96.810 97.555 X2 95.235 96.078 95.346 96.665 

and then the code is used to build.

+4
source share
2 answers

Sounds like a task for the grill:

 X <- as.data.frame(as.table(t_weekmean), stringsAsFactors=FALSE, responseName="values") X$variable <- as.numeric(gsub("^X","",X$variable)) X$Temp <- as.numeric(X$Temp) require(lattice) xyplot(values~variable|Week, groups=Temp, X, type="o", as.table=TRUE, xlab="Weight (gr)", ylab="Rate (umol/L*gr)", main="All individuals and Treatments at all times" ) 

Multi-plot in Lattice

I recreate your data as:

 t_weekmean <- structure(c(100, 98.855, 98.004, 95.953, 95.235, 100, 98.591, 97.804, 96.999, 96.078, 100, 98.357, 97.638, 96.81, 95.346, 100, 99.003, 98.299, 97.555, 96.665, 100, 99.137, 98.42, 97.939, 96.998, 100, 99.035, 98.298, 97.181, 96.237, 100, 97.883, 96.459, 94.406, 91.906, 100, 99.055, 97.765, 96.546, 95.263, 99.9889679441867, 98.8470416045204, 98.010997102523, 95.9636806506725, 95.235986063534, 100.00797414162, 98.5968712619705, 97.7984016535804, 96.9904933552904, 96.0816877686208, 99.9946318131395, 98.3568674165109, 97.6357767063124, 96.8119443900658, 95.3441814383421, 99.989633272252, 99.0037062049508, 98.3034580102509, 97.5568340624981, 96.6615796074679, 100.000379644977, 99.1375077671092, 98.4187321210541, 97.9350205929782, 97.0006243532971, 100.003971157774, 99.0316462150477, 98.298322594611, 97.1782003010139, 96.239865449585, 100.002464797458, 97.8810655647218, 96.4592857614756, 94.4099917372801, 91.9025173998885, 100.003642400375, 99.0529984607268, 97.76302246443, 96.5426428484451, 95.2658935513329), .Dim = c(5L, 4L, 4L), .Dimnames = structure(list(variable = c("X0", "X0.5", "X1", "X1.5", "X2"), Temp = c("9", "12", "15", "18"), Week = c("1", "2", "3", "4")), .Names = c("variable", "Temp", "Week")) ) 
+3
source

If you use plyr , you can do this with a_ply :

 a_ply(t_weekmean, 3, function(arrayforcurweek){ allweek1<-(data.frame(arrayforcurweek)) #which selects the date and generates the data frame I want to later format the date using week1<-stack(allweek1) #and then plot it using plot(week1$values,type="n", xlim=c(0,2),xlab="Weight (gr)",ylab="Rate (umol/L*gr)",main="All individuals and Treatments at all times") lines(week1$values[week1$ind=="X9"]~x,type="o",col="red") lines(week1$values[week1$ind=="X12"]~x,type="o",col="blue") lines(week1$values[week1$ind=="X15"]~x,type="o",col="green") lines(week1$values[week1$ind=="X18"]~x,type="o",col="purple") }) 

For example, you will only see the last chart, because the rest are usually overwritten. Thus, you can add layout instructions or provide a pause between graphs, etc.

OK, another info as per your comment:

a_ply takes 3 parameters here: first, an array for doing things, then "margin", which means: which of the dimensions to iterate over (this is a "hidden" loop) and, finally, a function to do all the parts.

So what happens: a_ply takes all possible values ​​for the third dimension of your array (because margin == 3) and works on them (you can see this as the index i in the for loop). Then it takes a part of your array for each of these values ​​(sort of like t_weekmean[,,i] ) and passes it to the function, which is the third parameter (therefore, inside this function consecutive marginal arrays will be known as arrayforcurweek).

The problem with this way of working is that the charts run quickly, so if you just run this code and look at the image window, you should see only the chart for the last value of your third dimension. If you want to see them all next to each other (although this will lead to tiny graphs), you must prefix: layout (matrix (1:30), nrow = 6) This will cause the screen to be divided by 30, so that each chart will receive its part of the full screen.

I believe that if you immediately write to pdf or similar, that you do not require it, but I have no experience with this.

Does this help you?

+3
source

All Articles