0

I have a dataset as I've shown below:

df_A <- tribble(
  ~id,       ~type,     ~min_price,  ~max_price,
  "1",        "X",          10,          40,
  "1",        "Y",          20,          50,
  "1",         NA,          15,          70,
  "1",        "X",          40,          90,
  "1",        "Y",          23,         100,
  "2",        "X",          18,          40,
  "2",        "Y",          34,          50,
  "2",        "Y",          64,         150,
  "2",         NA,          15,          70,
  "3",        "X",          40,          90,
  "3",        "Y",          23,         100,
)

Now, I want to manipulate the data to answer this question: "When "type" is X, what is the min price for each id?" or "when "type" is Y, what's min price for each id?"

desired_DF <- tribble(
  ~id,       ~type,     ~min_price,  ~max_price,
  "1",        "X",          10,          40,
  "1",        "Y",          20,          50,
  "2",        "X",          18,          40,
  "2",        "Y",          34,          50,
  "2",         NA,          15,          70,
  "3",        "X",          40,          90,
  "3",        "Y",          23,         100,
)

Could someone help me to get this?

datazang
  • 989
  • 1
  • 7
  • 20

2 Answers2

1

Such summary stats can be calculated with the dplyr package.

library(dplyr)

df_A %>%
  group_by(id, type) %>%
  summarise(min_price = min(min_price),
    max_price= max(max_price))
xilliam
  • 2,074
  • 2
  • 15
  • 27
0

something like this ?

df_A %>% filter(type=="X") %>% group_by(id) %>% summarize(m=min(min_price))
  • Code-only answers are generally frowned upon on this site. Could you please edit your answer to include some comments or explanation of your code? Explanations should answer questions like: What does it do? How does it do it? Where does it go? How does it solve OP's problem? See: [How to anwser](https://stackoverflow.com/help/how-to-answer). Thanks! – Eduardo Baitello Nov 28 '19 at 12:54