本文介绍了dplyr:case_when,涉及许多案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据框:

set.seed(002)
data1 <- data.frame(cbind(
  a1 = sample(letters, 8, replace = TRUE),
  a2 = rpois(8, 10)
), stringsAsFactors = FALSE)

data2 <- data.frame(cbind(
  b1 = paste("area", 1:6, sep = " "),
  b2 = c("e", "s", "o", "y", "d", "v")
), stringsAsFactors = FALSE)

data1
  a1 a2
1  e  9
2  s 10
3  o 12
4  e  9
5  y 16
6  y  9
7  d 11
8  v 13

data2
      b1 b2
1 area 1  e
2 area 2  s
3 area 3  o
4 area 4  y
5 area 5  d
6 area 6  v

我想在data1中创建一个称为a3的新列,同时将a1与data2中的信息进行匹配,例如,如果a1 ="e",则a3 =区域1",如果a1 ="d",则a3 =区域5",并且很快.新的data1应该看起来像这样:

I want to create a new column in data1 called a3 while matching a1 with information from data2 e.g if a1 = "e" then a3 = "area 1", if a1 = "d" then a3 = "area 5" and so on. The new data1 should look like this:

  a1 a2     a3
1  e  9 area 1
2  s 10 area 2
3  o 12 area 3
4  e  9 area 1
5  y 16 area 4
6  y  9 area 4
7  d 11 area 5
8  v 13 area 6

我可以做到这一点

data1 %>%
  mutate(a3 = case_when(
    a1 == "e" ~ "area 1",
    a1 == "s" ~ "area 2",
    a1 == "o" ~ "area 3",
    a1 == "y" ~ "area 4",
    a1 == "d" ~ "area 5",
    TRUE ~ "area 6"
  ))

问题是我有很多情况,我将在许多不同情况下的数据帧上重复此操作.

The problem is that I have many cases and I am to repeat this on a number of data frames with different cases.

我可以通过写以r为基

data1$a3 <- NA
for(i in 1:nrow(data2)){
  for(j in 1:nrow(data1)){
    if(data1[j,1] == data2[i,2]){
      data1[j,3] <- data2[i,1]
    }
  }
}

但是我很喜欢dplyr.感谢您提供有关使用dplyr实现此目标的任何帮助.

but I am a fun of dplyr. Any assistance on how to achieve this using dplyr is appreciated.

推荐答案

data1 <- dplyr::left_join(data1, data2, by = c("a1" = "b2"))

data1:-

a1 a2     b1
e  9   area 1
s 10   area 2
o 12   area 3
e  9   area 1
y 16   area 4
y  9   area 4
d 11   area 5
v 13   area 6

这篇关于dplyr:case_when,涉及许多案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 18:56