Working with arrays

I want to create a new variable "x" by comparing the values of a variable to those of several variables, for example, from the sample excel dataset, I want to do the following:

if (b<=perc1) { x=day1}
else if (b>perc[dim(k)-1]) {x=day[dim(k)] } # k=5 from the attached data as we have perc1 to perc5.
else for (b=2: dim(k)-1){
if (perc[b-1]<b<=perc[b] ) {x=day[b]}
}

Sample data:

perc1 perc2 perc3 perc4 perc5 day1 day2 day3 day4 day5 b
4 8 12 16 19 -196 -81 -66 -38 -28 1
4 8 12 16 19 -196 -81 -66 -38 -28 2
4 8 12 16 19 -196 -81 -66 -38 -28 3
4 8 12 16 19 -196 -81 -66 -38 -28 4
4 8 12 16 19 -196 -81 -66 -38 -28 5
4 8 12 16 19 -196 -81 -66 -38 -28 6
4 8 12 16 19 -196 -81 -66 -38 -28 7
4 8 12 16 19 -196 -81 -66 -38 -28 8
4 8 12 16 19 -196 -81 -66 -38 -28 9
4 8 12 16 19 -196 -81 -66 -38 -28 10
4 8 12 16 19 -196 -81 -66 -38 -28 11
4 8 12 16 19 -196 -81 -66 -38 -28 12
4 8 12 16 19 -196 -81 -66 -38 -28 13
4 8 12 16 19 -196 -81 -66 -38 -28 14
4 8 12 16 19 -196 -81 -66 -38 -28 15
4 8 12 16 19 -196 -81 -66 -38 -28 16
4 8 12 16 19 -196 -81 -66 -38 -28 17
4 8 12 16 19 -196 -81 -66 -38 -28 18
4 8 12 16 19 -196 -81 -66 -38 -28 19
4 8 12 16 19 -196 -81 -66 -38 -28 20
4 8 12 16 19 -196 -81 -66 -38 -28 21
4 8 12 16 19 -196 -81 -66 -38 -28 22
4 8 12 16 19 -196 -81 -66 -38 -28 23
4 8 12 16 19 -196 -81 -66 -38 -28 24
4 8 12 16 19 -196 -81 -66 -38 -28 25
4 8 12 16 19 -196 -81 -66 -38 -28 26
4 8 12 16 19 -196 -81 -66 -38 -28 27
4 8 12 16 19 -196 -81 -66 -38 -28 28

Sorry guys, I made a mistake in my original codes; Please see updated code below

if (b<=perc1) { x=day1}
else if (b>perc[dim(k)-1]) {x=day[dim(k)] } # k=5 from the attached data as we have perc1 to perc5.
else for (j=2: dim(k)-1){
if (perc[j-1]<b<=perc[j] ) {x=day[j]}
}

Does this do what you want?

library(tidyverse)
DF <- read.csv("~/R/Play/Dummy.csv", sep = " ")
DF <- DF |> mutate(x = case_when(
  b <= perc1 ~ day1,
  b > perc4 ~ day5,
  b > perc3 ~ day4,
  b > perc2 ~ day3,
  b > perc1 ~ day2
))
DF
#>    perc1 perc2 perc3 perc4 perc5 day1 day2 day3 day4 day5  b    x
#> 1      4     8    12    16    19 -196  -81  -66  -38  -28  1 -196
#> 2      4     8    12    16    19 -196  -81  -66  -38  -28  2 -196
#> 3      4     8    12    16    19 -196  -81  -66  -38  -28  3 -196
#> 4      4     8    12    16    19 -196  -81  -66  -38  -28  4 -196
#> 5      4     8    12    16    19 -196  -81  -66  -38  -28  5  -81
#> 6      4     8    12    16    19 -196  -81  -66  -38  -28  6  -81
#> 7      4     8    12    16    19 -196  -81  -66  -38  -28  7  -81
#> 8      4     8    12    16    19 -196  -81  -66  -38  -28  8  -81
#> 9      4     8    12    16    19 -196  -81  -66  -38  -28  9  -66
#> 10     4     8    12    16    19 -196  -81  -66  -38  -28 10  -66
#> 11     4     8    12    16    19 -196  -81  -66  -38  -28 11  -66
#> 12     4     8    12    16    19 -196  -81  -66  -38  -28 12  -66
#> 13     4     8    12    16    19 -196  -81  -66  -38  -28 13  -38
#> 14     4     8    12    16    19 -196  -81  -66  -38  -28 14  -38
#> 15     4     8    12    16    19 -196  -81  -66  -38  -28 15  -38
#> 16     4     8    12    16    19 -196  -81  -66  -38  -28 16  -38
#> 17     4     8    12    16    19 -196  -81  -66  -38  -28 17  -28
#> 18     4     8    12    16    19 -196  -81  -66  -38  -28 18  -28
#> 19     4     8    12    16    19 -196  -81  -66  -38  -28 19  -28
#> 20     4     8    12    16    19 -196  -81  -66  -38  -28 20  -28
#> 21     4     8    12    16    19 -196  -81  -66  -38  -28 21  -28
#> 22     4     8    12    16    19 -196  -81  -66  -38  -28 22  -28
#> 23     4     8    12    16    19 -196  -81  -66  -38  -28 23  -28
#> 24     4     8    12    16    19 -196  -81  -66  -38  -28 24  -28
#> 25     4     8    12    16    19 -196  -81  -66  -38  -28 25  -28
#> 26     4     8    12    16    19 -196  -81  -66  -38  -28 26  -28
#> 27     4     8    12    16    19 -196  -81  -66  -38  -28 27  -28
#> 28     4     8    12    16    19 -196  -81  -66  -38  -28 28  -28

Created on 2024-02-03 with reprex v2.0.2

This works perfect, thank you. However, what if I have several variables, e.g. perc1, perc2, ..., perc21 and day1, day2, ..., day21, is there a way to generalize the codes?

I can't find a way to automatically construct the conditions of the case_when(). It is easy enough to build a vector of strings that look like the correct formulas or to build a single string containing all of the formulas, but I can't find a way to convert them to formula objects that case_when() will take.

A for loop can be used to write a more flexible method .

DF <- read.csv("~/R/Play/Dummy.csv", sep = " ")
DF$x <- NA
for (i in 2:5) {
  DF$x <- ifelse(DF$b > DF[[paste0("perc", i-1)]], DF[[paste0("day", i)]], DF$x)
}
DF$x <- ifelse(DF$b <= DF$perc1, DF$day1, DF$x)

This works perfect. Thank you so much

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.