[datatable-help] How to implement function across groups and save unique output

bigappleanalyst aroos at terpmail.umd.edu
Sun Mar 4 01:06:47 CET 2018


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


More information about the datatable-help mailing list