简体   繁体   English

在 mutate 和 ifelse 中使用已排序变量的名称

[英]Using names of sorted variables within mutate and ifelse

I have following example data:我有以下示例数据:

id <- c(1, 2, 3)
ex3 <- c(0.8,   0.2, 0.3)
ex2 <- c(0.1,   0.4, 0.04)
ex1 <- c(0.04,  0.3, 0.5)
ex <- c(1, 1, 1)
ran <- c(0.5, 0.7, 0.6)
dat <- data.frame(id, ex1, ex2, ex3, ex, ran)

dat

  id  ex1  ex2 ex3 ex ran
1  1 0.04 0.10 0.8  1 0.5
2  2 0.30 0.40 0.2  1 0.7
3  3 0.50 0.04 0.3  1 0.6

I want to modify variable "ex" using following code with dplyr/tidyr:我想使用以下带有 dplyr/tidyr 的代码修改变量“ex”:

library(dplyr)
library(tidyr)

dat %>% 
  pivot_longer(
    cols = ex1:ex3
  ) %>% 
  arrange(id, desc(value)) %>% 
  group_by(id) %>% 
  mutate(ex = ifelse(ran <= value[1] & ran > sum(value[2], value[3]), 5, ex)) %>% 
  pivot_wider(
    names_from=name
  )

# A tibble: 3 x 6
# Groups:   id [3]
     id    ex   ran   ex3   ex2   ex1
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1     1     5   0.5   0.8  0.1   0.04
2     2     1   0.7   0.2  0.4   0.3 
3     3     1   0.6   0.3  0.04  0.5

Is it possible to use the names of "ex1"-"ex3" as new values for "ex" instead of "5" within the ifelse-statement in mutate?是否可以在 mutate 的 ifelse 语句中使用“ex1”-“ex3”的名称作为“ex”的新值而不是“5”? Example: Using the names of the ex$-variables as new values leads to this output:示例:使用 ex$-variables 的名称作为新值会导致此输出:

  id ex3  ex2  ex1  ex ran
1  1 0.8 0.10 0.04 ex3 0.5
2  2 0.2 0.40 0.30   1 0.7
3  3 0.3 0.04 0.50   1 0.6

Or using the number of the ex$-variables leads to this output:或者使用 ex$-variables 的数量导致此输出:

  id ex3  ex2  ex1  ex ran
1  1 0.8 0.10 0.04   3 0.5
2  2 0.2 0.40 0.30   1 0.7
3  3 0.3 0.04 0.50   1 0.6

Or if I want the lowest value as new value for "ex" (because it is "ex2"):或者,如果我想要最低值作为“ex”的新值(因为它是“ex2”):

  id ex3  ex2  ex1  ex ran
1  1 0.8 0.10 0.04   1 0.5
2  2 0.2 0.40 0.30   1 0.7
3  3 0.3 0.04 0.50   1 0.6

To sum it up: I want to refer to the variable-names of the sorted "ex$"-values to create new values for "ex" within ifelse in mutate.总结一下:我想参考已排序的“ex$”值的变量名,以在 mutate 中为 ifelse 中的“ex”创建新值。

One way could be using parse_number from readr package that extracts the numbers from ex1, ex2, ex3.一种方法是使用parse_number包中的readr从 ex1、ex2、ex3 中提取数字。 Depending on the logic you can do:根据您可以执行的逻辑:

parse_number(name[1]) here 1 is the position in the column you can use 2 or 3 dependig what fits best your logic. parse_number(name[1])此处 1 是列中的位置,您可以使用 2 或 3 取决于最适合您的逻辑的位置。

library(dplyr)
library(tidyr)
library(readr)

dat %>% 
  pivot_longer(
    cols = ex1:ex3
  ) %>% 
  arrange(id, desc(value)) %>% 
  group_by(id) %>% 
  mutate(ex = ifelse(ran <= value[1] & ran > sum(value[2], value[3]), parse_number(name[3]), ex)) %>% 
  pivot_wider(
    names_from=name
  )

   id    ex   ran   ex1   ex2   ex3
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1     1     3   0.5   0.8  0.1   0.04
2     2     1   0.7   0.2  0.4   0.3 
3     3     1   0.6   0.3  0.04  0.5 

For full name:对于全名:

mibrary(dplyr)
library(tidyr)
library(readr)

dat %>% 
  pivot_longer(
    cols = ex1:ex3
  ) %>% 
  arrange(id, desc(value)) %>% 
  group_by(id) %>% 
  mutate(ex = ifelse(ran <= value[1] & ran > sum(value[2], value[3]), name[1], as.character(ex))) %>% 
  pivot_wider(
    names_from=name
  )
     id ex      ran   ex1   ex2   ex3
  <dbl> <chr> <dbl> <dbl> <dbl> <dbl>
1     1 ex1     0.5   0.8  0.1   0.04
2     2 1       0.7   0.2  0.4   0.3 
3     3 1       0.6   0.3  0.04  0.5 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM