Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to cluster or group days and write it to a .csv file in R?
    text
    copied!<p>I have this enormous df with measurements over 10 days. Now I need to get variances and repeatabilities for the entire dataset, single days, and clusters of days. It was rather easy to do it for the entire dataset. For the single days I created the following loop (which worked btw):</p> <pre><code>All_D &lt;- unique(lam$Start_date) for (d in 1:10){ jaj.d &lt;- All_D[d] Days.d &lt;- subset(lam, Start_date == jaj.d) jaa &lt;- as.data.frame(as.table(with(Days.d, tapply(CH4, ID, FUN = var)))) names(jaa) &lt;- c("ID", "within_ani") write.csv(jaa, paste("Day_",jaj.d,".csv",sep = ""),row.names = F) } </code></pre> <p>Now I would like to make groups of two days which "walk through" the 10 days, but they have to stay clustered.. So like:</p> <blockquote> <p>2013-09-01 &amp; 2013-09-02, 2013-09-02 &amp; 2013-09-03, 2013-09-03 &amp; 2013-09-04, .. ,<br> 2013-09-09 &amp; 2013-09-10</p> </blockquote> <p>I think it is necessary to create another loop, but (except for the information above) I have no clue where to start.. I also have to group 3 - 9 days, so I'd rather not do it by hand! I have a df which looks as follows:</p> <pre><code>'data.frame': 1420847 obs. of 22 variables: $ ID : int 12338 12338 12338 12338 12338 12338 12338 12338 12338 12338 ... $ CO2 : int 1510 1950 1190 1170 780 870 730 740 680 700 ... $ CH4 : int 66 77 62 58 34 51 36 43 32 40 ... $ Start_date: chr "2013-09-01" "2013-09-01" "2013-09-01" "2013-09-01" ... </code></pre> <p>I am kind of a noob concerning R, and I was hoping someone could give me a nudge in the right direction? I have been struggling with this for a couple of hours and I cannot seem to find a solution on this website or somewhere else on the web. English is not my native language and I find it kind of hard to come up with the right search terms, so it really is not for lack of trying.</p> <p>If my question is still unclear, let me know and I'll try to adjust it.</p> <p><strong>EDIT</strong></p> <p>Soooooo, with the help of you guys I came up with this loop:</p> <pre><code>&gt; lam &lt;- df lam$Start_date &lt;- as.Date(lam$Start_date) require(data.table) lam &lt;- as.data.table(lam) #transform df to dt lam[,date1 := c(1, diff(Start_date))] #assign each date a different number lam[,date1 := cumsum(date1)] for (i in 1:10) { #loop through each level of date lap.i &lt;- split(lam, lam$date1) #split date1 to get single days for (j in 1:(i+1)) { #loop through each level of date lap.j &lt;- split(lam, lam$date1) #split date1 to get the day after i } for (k in (i+2)) { lap.k &lt;- split(lam, lam$date1) #split date1 to get the day after j } for (l in 1:(i+3)) { lap.l &lt;- split(lam, lam$date1) #split date1 to get the day after k } lap.i.j.k.l &lt;- rbind(lap[[i]], lap[[j]], lap[[k]], lap[[l]]) #binding the lists together var.i.j.k.l &lt;- var(lap.i.j.k.l$CH4) #get the between individual variance for CH4 #get a df with individual variances for CH4 kill &lt;- as.data.frame(c(with(lap.i.j.k.l, tapply(CH4, CowID, FUN = var)), var.i.j.k.l)) names(kill) &lt;- c("variance") #name columns in df #write to a .csv file in wd write.csv(kill, paste("consecutive days_", i, "_", j, "_", k, "_", l, ".csv", sep = "")) } </code></pre> <p>This does exactly what I want, however, R is not a big fan of a loop inside a loop inside a loop and so on. The loop above is to get tables with the data of 4 consecutive days, I need to go until 9 consecutive days.. Since the loop above is already asking a lot of this computer, I was wondering what is a shorter, easier, more efficient way to accomplish this? Not "if" because I know it exists, Codoremifa already kind of showed me that, it's just that his code did not exactly do what I want, and I can't seem to figure out how it precisely works.. </p> <p><strong>EDIT 2</strong><br> What I am trying to accomplish:</p> <pre><code> ID CO2 CH4 dates date1 1 12338 1510 66 2013-09-01 1 2 12338 1950 77 2013-09-01 1 3 12338 1190 62 2013-09-01 1 4 12338 1170 58 2013-09-02 1 5 12338 780 34 2013-09-02 1 6 12338 870 51 2013-09-03 2 7 12338 1670 66 2013-09-03 2 8 12338 1980 77 2013-09-03 2 9 12338 1330 62 2013-09-04 2 10 12338 1850 58 2013-09-04 2 11 12338 1640 34 2013-09-05 3 12 12338 590 51 2013-09-05 3 </code></pre> <p>And after that lists like:</p> <pre><code>&gt; [1] ID var 12338 164077.4 12339 78420.31 12352 91472.76 &gt; [2] ID var 12338 33543.16 12339 184467.1 12352 202267.3 </code></pre> <p>Which I would like to write to a .csv file</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload