Search code examples
mysqlrdataframefor-loopdatatable

How to copy data from one column to a new column based on price range in R?


Hello, i have a dataset on shoes prices from amazon which look like this

Brand Available Price color
Nike Yes $50 Red
Nike Yes $40 - $50 RED
Adidas Yes $46 - $90 White
Puma Yes NAN White

My task is, if there are two prices like for second and third row then create a new row in table and put the second price, and if price is NAN then delete that column too.

I am new to R language and trying to solve this from 3 days, my task is to show mean prices of different brands in R with graphs like scatter plot graph, i am expecting the output like this:

Brand Available Price color
Nike Yes $50 Red
Nike Yes $40 RED
Adidas Yes $46 White
Nike Yes $50 RED
Adidas Yes $90 White

i tried different code but nothing is working can someone please help me with this.

This is my dataset


Solution

  • We could use separate_rows with the - separator and omit NAs:

    library(dplyr)
    library(tidyr)
    
    df %>% 
      separate_rows(Price, sep=" - ") %>% 
      na.omit()
    
      Brand  Available Price color
      <chr>  <chr>     <chr> <chr>
    1 Nike   Yes       $50   Red  
    2 Nike   Yes       $40   RED  
    3 Nike   Yes       $50   RED  
    4 Adidas Yes       $46   White
    5 Adidas Yes       $90   White