Separate and Manipulate Rows by Rule

Hi,

I have a DF:

1 119629-19
2 3,750 TL
3 70383-4024
4 48256-550
5 1,300 TL
6 9842-4913
7 104146-243
8 63839-1196
9 1,500 TL
10 1718-1974
11 1718-1973
12 119808-7
13 3,000 TL
14 2957-714
15 5,500 TL
16 117842-24
17 1718-1948
18 1718-1949
19 1718-1760
20 1718-1966
21 1718-1801
22 1718-1829
23 119257-4
24 119257-1

As you can see, numeric lines are ID and others are Price. Every price is the price of the upper ID's price.

I want to create a DF as:

ID Price
119629-19 3,750 TL
70383-4024 NULL
48356-550 1,300 TL
9842-4913 NULL
104146-243 NULL
63839-1196 1,500 TL

...

Thank you!

example_df <- function(intext) {
tf <- tempfile()
writeLines(intext, con = tf)
require(tidyverse)
as_tibble(read.delim(tf,sep=";"))
}
(df <- example_df("
row;value
1;119629-19
2;3,750 TL
3;70383-4024
4;48256-550
5;1,300 TL
6;9842-4913
7;104146-243
8;63839-1196
9;1,500 TL
10; 1718-1974
11; 1718-1973
12; 119808-7
13; 3,000 TL
14; 2957-714
15; 5,500 TL
16; 117842-24
17; 1718-1948
18; 1718-1949
19; 1718-1760
20; 1718-1966
21; 1718-1801
22; 1718-1829
23; 119257-4
24; 119257-1"))

(idlist <- filter(df,
                 str_detect(value,"TL",negate = TRUE)) %>% mutate(
                   nextidrow = lead(row)
                 ))
(pricelist <- filter(df,
                 str_detect(value,"TL",negate = FALSE)))

library(fuzzyjoin)

fuzzyjoin::fuzzy_left_join(idlist,
                           pricelist,
                           by=c("row"="row",
                                "nextidrow"="row"),
                           match_fun = list(
                             `<`,`>`
                           )) %>%
  select(ID=value.x,
         price =value.y)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.