![](/img/trans.png)
[英]How to apply a function to mutate a specific combination of columns? (purrr:: use preferred)
[英]How to apply function to pairs of columns with purrr?
我有以下与dplyr联接产生的数据框:
data_frame(id=1:4, a.x = c(1, NA, 3, 4), a.y = c(1, 2, 3, 4), b.x = c(NA, NA, 3, NA), b.y = c(2, 2, NA, 4))
# A tibble: 4 x 5
id a.x a.y b.x b.y
<int> <dbl> <dbl> <dbl> <dbl>
1 1 1 1 NA 2
2 2 NA 2 NA 2
3 3 3 3 3 NA
4 4 4 4 NA 4
我想用以.y
结尾的列中的值替换以.x
结尾的列中的所有NA。 最终,我想实现以下目标:
# A tibble: 4 x 5
id a.x a.y b.x b.y
<int> <dbl> <dbl> <dbl> <dbl>
1 1 1 1 2 2
2 2 2 2 2 2
3 3 3 3 3 NA
4 4 4 4 4 4
我用purrr尝试了这样的事情:
data_frame(id=1:4, a.x = c(1, NA, 3, 4), a.y = c(1, 2, 3, 4), b.x = c(NA, NA, 3, NA), b.y = c(2, 2, NA, 4)) %>%
map2_dfr(.x = ends_with('.y'), .y = ends_with('.x'), ~ case_when(is.na(.x) ~ .y,
TRUE ~ .x))
哪有错 文档对我来说有些混乱,我认为这里的问题是.x需要一个向量,但是我怎样才能传递列列表呢?
泰迪解决方案。 我们可以gather
以separate
的列.
,按列arrange
,向上fill
值, unite
列,最后将数据帧spread
到原始结构。
library(tidyverse)
dat2 <- dat %>%
gather(Column, Value, -id) %>%
separate(Column, into = c("Col1", "Col2")) %>%
arrange(id, Col1, Col2) %>%
group_by(id, Col1) %>%
fill(Value, .direction = "up") %>%
unite(Column, Col1, Col2, sep = ".") %>%
spread(Column, Value) %>%
ungroup()
dat2
## A tibble: 4 x 5
# id a.x a.y b.x b.y
# * <int> <dbl> <dbl> <dbl> <dbl>
# 1 1 1.00 1.00 2.00 2.00
# 2 2 2.00 2.00 2.00 2.00
# 3 3 3.00 3.00 3.00 NA
# 4 4 4.00 4.00 4.00 4.00
或者,如果数据框中各列的顺序正确,则可以使用data.table包中的transpose
函数,但请注意,此过程后列类型可能会更改。
dat2 <- dat %>%
data.table::transpose() %>%
fill(everything(), .direction = 'up') %>%
data.table::transpose() %>%
setNames(names(dat))
dat2
# id a.x a.y b.x b.y
# 1 1 1 1 2 2
# 2 2 2 2 2 2
# 3 3 3 3 3 NA
# 4 4 4 4 4 4
或使用purrr的解决方案来创建子集,该子集的列名称ends_with
“ x”和“ y”,然后将原始列替换为“ x”。
dat_x <- dat %>% select(ends_with("x"))
dat_y <- dat %>% select(ends_with("y"))
dat[, grepl("x$", names(dat))] <- map2(dat_x, dat_y, ~ifelse(is.na(.x), .y, .x))
dat
# # A tibble: 4 x 5
# id a.x a.y b.x b.y
# <int> <dbl> <dbl> <dbl> <dbl>
# 1 1 1.00 1.00 2.00 2.00
# 2 2 2.00 2.00 2.00 2.00
# 3 3 3.00 3.00 3.00 NA
# 4 4 4.00 4.00 4.00 4.00
数据
dat <- data_frame(id=1:4, a.x = c(1, NA, 3, 4), a.y = c(1, 2, 3, 4), b.x = c(NA, NA, 3, NA), b.y = c(2, 2, NA, 4))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.