R dplyr - Add Column Based on Results in Other Rows -


i have list of item numbers , inventory locations. items can appear in 2 types of inventory locations - warehouse or par locations. add column data indicate if particular item number ever associated warehouse location. sample data below:

item_num <- c("item - 1", "item - 2", "item - 3", "item - 1", "item - 3",            "item - 2") locs <- c("warehouse", "par", "par", "par", "warehouse", "par") fake_data <- tibble(item_num, locs)  fake_data # tibble: 6 x 2   item_num      locs      <chr>     <chr> 1 item - 1 warehouse 2 item - 2       par 3 item - 3       par 4 item - 1       par 5 item - 3 warehouse 6 item - 2       par 

i add column true item - 1 , item - 3 since there warehouse location both of in data. item - 2 false. this:

  item_num      locs wh_exists 1 item - 1 warehouse      true 2 item - 2       par     false 3 item - 3       par      true 4 item - 1       par      true 5 item - 3 warehouse      true 6 item - 2       par     false 

my messy solution below.

wh_locs <- fake_data %>%   filter(locs == "warehouse") fake_data$wh_exist <- fake_data$item_num %in% wh_locs$item_num  fake_data  # tibble: 6 x 3   item_num      locs wh_exist      <chr>     <chr>    <lgl> 1 item - 1 warehouse     true 2 item - 2       par    false 3 item - 3       par     true 4 item - 1       par     true 5 item - 3 warehouse     true 6 item - 2       par    false 

this works, seems me there should clever way use mutate , group_by answer can keep in 1 piped set of functions.

thank you.

we can use any ==

fake_data %>%       group_by(item_num) %>%        mutate(wh_exists = any(locs == "warehouse")) #     item_num      locs wh_exists #     <chr>     <chr>     <lgl> #1 item - 1 warehouse      true #2 item - 2       par     false #3 item - 3       par      true #4 item - 1       par      true #5 item - 3 warehouse      true #6 item - 2       par     false 

a similar option using data.table

library(data.table) setdt(fake_data)[, wh_exists := any(locs == "warehouse"), = item_num] 

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -