我有两个数据框:
f
我想在data1中创建一个名为a3的新列,同时将a1与来自data2的信息匹配,例如,如果a1 =“e”,则a3 =“area 1”,如果a1 =“d”则a3 =“area 5”,等等。新数据1应如下所示:
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
我可以通过
实现这一目标 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"
))
但我很喜欢dplyr。有关如何使用dplyr实现此目的的任何帮助表示赞赏。
答案 0 :(得分:1)
看起来你真的只是在这种情况下进行联接。你可以做到
left_join(data1, data2, by=c("a1"="b2"))
# a1 a2 b1
# 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
如果你关心重新命名列,或者只关注data2中的一个特定列,那么你可以做到
left_join(data1, data2 %>% select(a1=b2, a3=b1))
答案 1 :(得分:1)
此解决方案不使用dplyr::case_when()
,但它确实使用了另一个dplyr
函数。您可以直接加入表格,然后使用case_when()
重命名变量,而不是使用第二个表格来编写dplyr::rename()
。
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
set.seed(2)
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 %>% left_join(data2, by = c(a1 = "b2")) %>% rename(a3 = b1)
#> 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
答案 2 :(得分:1)
function concatenate(){
document.getElementById("result1").innerHTML =
document.getElementById("text_area_1").value + " " +
document.getElementById("text_area_2").value;
document.getElementById("result2").innerHTML =
document.getElementById("text_area_3").value;
}
$(document).ready(function(){
var arr = ["One","Two","Three"];
var index = 0;
$('#next').click(function(){
index = (index + 1) % arr.length ;
});
});
DATA1: -
data1 <- dplyr::left_join(data1, data2, by = c("a1" = "b2"))