<div dir="ltr">Hi,<div><br></div><div>This is a mailing list related to the data.table package, not general R help. See <a href="https://www.r-project.org/help.html">https://www.r-project.org/help.html</a> </div><div><br></div><div>If you're interested in dplyr, its forum might be a good place to learn: <a href="https://community.rstudio.com/t/faq-whats-a-reproducible-example-reprex-and-how-do-i-do-one/5219">https://community.rstudio.com/t/faq-whats-a-reproducible-example-reprex-and-how-do-i-do-one/5219</a></div><div><br></div><div>Best,</div><div><br></div><div>Frank</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Mar 3, 2018 at 7:06 PM, bigappleanalyst <span dir="ltr"><<a href="mailto:aroos@terpmail.umd.edu" target="_blank">aroos@terpmail.umd.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I have a flat file that I imported into R. The file is 10,000 rows. In the<br>
file there is a date variable, and a group variable. There are 100 groups,<br>
so the group variable shows a value of 1 for rows 1:100, a value of 2 for<br>
rows 101:200, and so on for all 100 groups. The date variable has 100 unique<br>
entries, and repeats/restarts for each group. Thus, the dates in rows 1:100<br>
are identical to the dates in rows 101:200, 201:300, and so on.<br>
<br>
I wrote a function that manipulates a vector in the flat file, vector_1. As<br>
it stands, it manipulates vector_1 across all 10,000 rows. Vector_1 has<br>
unique entries in rows 1:100, 101:200, 201:300, and so on. Thus, I would<br>
like to implement a parameter so the function only calculates across the<br>
specified range. If I input group = 1, then the function will only perform<br>
the calculation on vector_1 for rows 1:100.<br>
<br>
Here is my function:<br>
<br>
adjusted <- c()<br>
Adjustment <- function(delta, length) {<br>
adjusted <<- vector_1 + delta*(index <= length)<br>
head(adjusted)<br>
}<br>
<br>
Here is a hard-coded example of what I would like to achieve:<br>
<br>
adjusted <- c()<br>
Adjustment <- function(delta, length, group = 1) {<br>
adjusted <<- vector_1[1:100] + delta*(index <= length)<br>
head(adjusted)<br>
}<br>
<br>
I would like to implement a parameter or a loop that performs the<br>
calculation over the corresponding range of vector_1 for the inputted group<br>
parameter value. For example, if I instead entered group =2 the function<br>
would look like:<br>
<br>
adjusted <- c()<br>
Adjustment <- function(delta, length, group = 2) {<br>
adjusted <<- vector_1[101:200] + delta*(index <= length)<br>
head(adjusted)<br>
}<br>
<br>
How would I achieve this? How do I manipulate the original group, date, and<br>
vector_1 vectors to allow for this modular calculation?<br>
<br>
Finally, I would like the function to store a unique "adjusted" vector for<br>
all 100 groups. For example, adjusted_1, adjusted_2, and so on...<br>
<br>
I found that the dplyr package may be useful for this, but I haven't been<br>
successful in implementing it.<br>
<br>
Any insight would be much appreciated!<br>
<br>
<br>
<br>
--<br>
Sent from: <a href="http://r.789695.n4.nabble.com/datatable-help-f2315188.html" rel="noreferrer" target="_blank">http://r.789695.n4.nabble.com/<wbr>datatable-help-f2315188.html</a><br>
______________________________<wbr>_________________<br>
datatable-help mailing list<br>
<a href="mailto:datatable-help@lists.r-forge.r-project.org">datatable-help@lists.r-forge.<wbr>r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help" rel="noreferrer" target="_blank">https://lists.r-forge.r-<wbr>project.org/cgi-bin/mailman/<wbr>listinfo/datatable-help</a><br>
</blockquote></div><br></div>