Theorp
Theorp

Reputation: 151

how to use if in two dataset at the same time - R

One doubt, I need to compare two datasets by the same id number between them, and if it is the same, I need to divide a field in column x, by the field in column y.

ex.:

tableX <- as_tibble (tableX)
tableY <- as_tibble (tableY)

if (tableX $ id == tableY $ id) {
  tableX $ example <- tableY $ px / tableY $ px
}

Since they are two separate datasets, I don't know how to do this filter because of the number of rows in each dataset ([i]). If anyone knows any function or anything that helps me, I am very grateful.

Upvotes: 0

Views: 34

Answers (1)

Ben Norris
Ben Norris

Reputation: 5747

A join is probably what you want. Then you can filter and compute whatever you need to.

library(dplyr)
# if you only want those rows with matching IDs in both tables
# Inner_join matches up the id values
tableX %>% 
  innner_join(tableY, by = "id") %>%
  mutate(example = ...)  # Where ... is the calculation you want to do

# If you want to keep all the rows in X even if there is not a match in Y
tableX %>%
  left_join(tableY, by = "id") %>% 
   mutate(example = ...)  # Where ... is the calculation you want to do

# If you want to do the left_join and have different behavior id there is a match or not
tableX %>%
  left_join(tableY, by = "id") %>% 
   mutate(example = case_when(is.na(table.Y.px) ~ ...,  # Where ... is the calculation you want to do
                              TRUE ~ ...) # How you want the rest of the cases to be treated

Upvotes: 1

Related Questions