Home > Enterprise >  dplyr indexing from an external dataframe resulting in incoherent values
dplyr indexing from an external dataframe resulting in incoherent values

Time:10-13

I have a dataframe with values x and want to find the interval they belong to which is stored in limit$interval. Then I want to replace those values with the limit$target value. E.g. the first value which is 1.4509 should be 1.5 and so on. The problem is, that there are values like df_target[64,] which has an x-value of 1.2926 for which the correct limit_index (1) is found, but the target results in 3.5 which is clearly not what I intended. Why? Here's a reprex:

require(dplyr)
require(tibble)

set.seed(123)

x <- runif(100, -2, 10)
limit <- data.frame(interval = 1:4, target = 1:4   0.5)
limit_index <- findInterval(x, limit$interval)
df <- data.frame(x = x, limit_index = limit_index)
df_target <- df %>% mutate(target = ifelse(limit_index == 0, x, limit$target[limit_index]))

CodePudding user response:

Probably due to the length of limit$target[limit_index]) being 74. If you use if_else() from dplyr in stead of ifelse() it throws an error.

Here is a suggestion to a solution:

df %>% mutate(target = case_when(limit_index == 0 ~ x,
                                         limit_index == 1 ~ 1.5,
                                         limit_index == 2 ~ 2.5,
                                         limit_index == 3 ~ 3.5,
                                         limit_index == 4 ~ 4.5))

CodePudding user response:

Here is a solution using a left_join:

df %>% 
  left_join(limit, by = c("limit_index" = "interval")) %>% 
   mutate(target = ifelse(is.na(target), x, target))

Which gives us:

            x limit_index     target
1   1.4509302           1  1.5000000
2   7.4596616           4  4.5000000
3   2.9077231           2  2.5000000
4   8.5962088           4  4.5000000
5   9.2856074           4  4.5000000
6  -1.4533220           0 -1.4533220
7   4.3372659           4  4.5000000
8   8.7090285           4  4.5000000
9   4.6172202           4  4.5000000
10  3.4793768           3  3.5000000
11  9.4820001           4  4.5000000
12  3.4400099           3  3.5000000
13  6.1308476           4  4.5000000
14  4.8716008           4  4.5000000
15 -0.7649038           0 -0.7649038
16  8.7978996           4  4.5000000
17  0.9530528           0  0.9530528
18 -1.4952856           0 -1.4952856
19  1.9350486           1  1.5000000
20  9.4540438           4  4.5000000
  • Related