0

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)
}

Actual question:

How would I achieve this?

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!

datanalyst
  • 351
  • 1
  • 3
  • 15
  • There is a direct mapping between `group` and the indices you pass to `vector_1`. Can't you just write `vector_1[(100*(group-1) + 1):100*group]`? – ruaridhw Mar 04 '18 at 00:19
  • Can you just add few rows of data and expected output ? I suspect there's a simpler solution to this. – YOLO Mar 04 '18 at 00:22
  • @ManishSaraswat If you click on the hyperlink you'll see a few rows of data. – datanalyst Mar 04 '18 at 00:27
  • @ruaridhw there is no direct mapping. – datanalyst Mar 04 '18 at 00:35
  • 1
    and the output ? Please refer to this guide to get quick and suitable solutions: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – YOLO Mar 04 '18 at 00:38

2 Answers2

0

I am not sure what delta index or length are here so I have made a few assumptions. Also since you have not provided any dummy data I have made some up.

library(tidyverse)

# dummy data
dts <- rep(1:100,100)
region <- as.factor(rep(1:100, each = 100))
vec1 <- 1:10000
df <- as.data.frame(cbind(dts, region, vec1))

adjusted <- c()

Adjustment <- function(df, index, delta, length, selectRegion= 2) {
  subDf <- df %>%
    filter(region == selectRegion)
  adjusted <<- subDf$vec1 + delta*(index <= length)
  head(adjusted)
}

Adjustment(df, index = 2, delta = 2, length = 2, selectRegion = 4)


## run function for all levels of selectRegion
res <- lapply(levels(region), Adjustment, df = df, index = 2, delta = 2, length = 2)

## add names to list elements
names(res) <- paste0("adjustment_", 1:length(res))

res

$adjustment_1
[1] 3 4 5 6 7 8

$adjustment_2
[1] 103 104 105 106 107 108

$adjustment_3
[1] 203 204 205 206 207 208

$adjustment_4
[1] 303 304 305 306 307 308

$adjustment_5
[1] 403 404 405 406 407 408

 Output keeps going to the 100th region. 
flee
  • 1,253
  • 3
  • 17
  • 34
  • Can you explain the importance of the factors? Also, let me clarify. length is a parameter that makes the adjustments to the original vector_1 for all elements less than or equal to length. delta is how to change the vector_1, either add or subtract. – datanalyst Mar 04 '18 at 00:59
  • The `fctr` variable is your `region` variable, I will change that in my answer. You can change `delta` and `length` in my answer to whatever you like. You really should follow the guidelines here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example if you want to get a quick and accurate help. – flee Mar 04 '18 at 01:07
0

To get the result shown in the attached image, the easiest solution would be to get the minimal value for each region and subtract that from all entries in the region, then add 1:

minimalValues <- tapply(yourData$index_value, yourData$region, min)
index_value2 <- yourData$index_value - minimalValues[yourData$region] + 1

If you wanted to make this "modular" you could define a function like

resetDate <- function(regn, yourData) {
    vals <- yourData$index_value[yourData$region == regn]
    yourData$index_value[yourData$region == regn] <- vals - min(vals) + 1
}
Richard Border
  • 3,209
  • 16
  • 30