[datatable-help] How to implement function across groups and save unique output
Frank Erickson
by.hook.or at gmail.com
Sun Mar 4 01:51:33 CET 2018
Hi,
This is a mailing list related to the data.table package, not general R
help. See https://www.r-project.org/help.html
If you're interested in dplyr, its forum might be a good place to learn:
https://community.rstudio.com/t/faq-whats-a-reproducible-example-reprex-and-how-do-i-do-one/5219
Best,
Frank
On Sat, Mar 3, 2018 at 7:06 PM, bigappleanalyst <aroos at terpmail.umd.edu>
wrote:
> I have a flat file that I imported into R. The file is 10,000 rows. In the
> file there is a date variable, and a group variable. There are 100 groups,
> so the group variable shows a value of 1 for rows 1:100, a value of 2 for
> rows 101:200, and so on for all 100 groups. The date variable has 100
> unique
> entries, and repeats/restarts for each group. Thus, the dates in rows 1:100
> are identical to the dates in rows 101:200, 201:300, and so on.
>
> I wrote a function that manipulates a vector in the flat file, vector_1. As
> it stands, it manipulates vector_1 across all 10,000 rows. Vector_1 has
> unique entries in rows 1:100, 101:200, 201:300, and so on. Thus, I would
> like to implement a parameter so the function only calculates across the
> specified range. If I input group = 1, then the function will only perform
> the calculation on vector_1 for rows 1:100.
>
> Here is my function:
>
> adjusted <- c()
> Adjustment <- function(delta, length) {
> adjusted <<- vector_1 + delta*(index <= length)
> head(adjusted)
> }
>
> Here is a hard-coded example of what I would like to achieve:
>
> adjusted <- c()
> Adjustment <- function(delta, length, group = 1) {
> adjusted <<- vector_1[1:100] + delta*(index <= length)
> head(adjusted)
> }
>
> I would like to implement a parameter or a loop that performs the
> calculation over the corresponding range of vector_1 for the inputted group
> parameter value. For example, if I instead entered group =2 the function
> would look like:
>
> adjusted <- c()
> Adjustment <- function(delta, length, group = 2) {
> adjusted <<- vector_1[101:200] + delta*(index <= length)
> head(adjusted)
> }
>
> How would I achieve this? How do I manipulate the original group, date, and
> vector_1 vectors to allow for this modular calculation?
>
> Finally, I would like the function to store a unique "adjusted" vector for
> all 100 groups. For example, adjusted_1, adjusted_2, and so on...
>
> I found that the dplyr package may be useful for this, but I haven't been
> successful in implementing it.
>
> Any insight would be much appreciated!
>
>
>
> --
> Sent from: http://r.789695.n4.nabble.com/datatable-help-f2315188.html
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/
> listinfo/datatable-help
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20180303/8976926a/attachment.html>
More information about the datatable-help
mailing list