Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using purrr map to apply function to selection of columns in DataFrame in dplyr pipeline

Tags:

r

purrr

tidyverse

I have the following dataframe

test <- data.frame(x = c(6, 9, 3, NA),
                   y = c(3, NA, 2, 3),
                   z = c(6, 3, NA, 5),
                   h = c(NA, 6, 7, 2))

This is the list of columns i would like to iterate over

mylist <- list(test$y, test$z)

I want to change columns "y" and "z" based on the condition in ifelse

Here is my attempt...seems not to work

test <- test %>%
          map_df(mylist, if(is.na(mylist), 0, 1))

(in reality i have a much larger dataframe, this is just test data)

Do I need to use mutate?
Can I use select in the pipeline? like this?

test <- test %>%
          map_df(select(y, z), if(is.na(.), 0, 1))

Here is the expected output

test <- data.frame(x = c(6, 9, 3, NA),
                   y = c(1, 0, 1, 1),
                   z = c(1, 1, 0, 1),
                   h = c(NA, 6, 7, 2))

Thanks for the help

like image 966
E50M Avatar asked Oct 17 '25 17:10

E50M


1 Answers

We can use mutate_at to specify columns

library(dplyr)
test %>%  mutate_at(vars(y, z), ~as.integer(!is.na(.)))

#   x y z  h
#1  6 1 1 NA
#2  9 0 1  6
#3  3 1 0  7
#4 NA 1 1  2

Or if ifelse is preferred

test %>% mutate_at(vars(y, z), ~ifelse(is.na(.), 0, 1))

We can also do the same in base R

cols <- c("y", "z")
test[cols] <- as.integer(!is.na(test[cols]))
like image 199
Ronak Shah Avatar answered Oct 20 '25 07:10

Ronak Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!