-1
Des          Price                                New column 

a   27.82 / 27.82 / 23.65 / 27.82                   27.82 / 23.65 / 27.82

b   19.87 / 19.87 / 19.14 / 19.87                   19.87 / 19.14 / 19.87

c   32.25 / 32.25 / 31 / 32.25 / 31                 32.25 / 31 / 32.25 / 31

d   79.39 / 79.39 / 79.39 / 79.39 / 83.36 / 79.39   79.39 / 83.36 / 79.39

I have a price column . I need new column only eliminating continuous repititve values. PLease suggest. I first row, since 27.82 is continuously repitive, it should be made as 1 and the out put should be 27.82/23.65/27.82

pogibas
  • 27,303
  • 19
  • 84
  • 117
  • 2
    Welcome to SO! Please add example of your data using `dput` function (`dput(head(DATA))`). – pogibas Sep 12 '18 at 13:08
  • 2
    Possible duplicate of [Remove/collapse consecutive duplicate values in sequence](https://stackoverflow.com/questions/27482712/remove-collapse-consecutive-duplicate-values-in-sequence) – LAP Sep 12 '18 at 13:12
  • I my case it is seperated by '/'. I am not getting –  Sep 12 '18 at 15:51

3 Answers3

0
test=c("27.82 / 27.82 / 23.65 / 27.82","19.87 / 19.87 / 19.14 / 19.87","32.25 / 32.25 / 31 / 32.25 / 31","79.39 / 79.39 / 79.39 / 79.39 / 83.36 / 79.39")
unlist(lapply(strsplit(test," / "),function(x) paste(rle(x)$values,collapse=" / ")))

Basically I built your Price vector in test. What's going on here is:

  1. I divide each row of your 1-column vector into its parts with strsplit.
  2. To each of those elements, I check for the repeated contiguous elements (like Unix uniq) using rle, which generates a list with a element called values in which the uniqed values are stored.
  3. I paste all these values with paste, collapse option is used to set the separator to the same that was being used before.
  4. As this has generated a list, I unlist it to generate a column.

Output:

> unlist(lapply(strsplit(test," / "),function(x) paste(rle(x)$values,collapse=" / ")))
[1] "27.82 / 23.65 / 27.82"   "19.87 / 19.14 / 19.87"   "32.25 / 31 / 32.25 / 31" "79.39 / 83.36 / 79.39"  

By the way, next time it would be better if you provided the community with a reproducible example (so readers don't have to write your data into a object by themselves), plus show what have you tried so far.

Carles Borredá
  • 358
  • 2
  • 8
0

Here is a slightly different approach, using tidyverse:

Data

vect <- c('27.82 / 27.82 / 23.65 / 27.82',
          '19.87 / 19.87 / 19.14 / 19.87',
          '32.25 / 32.25 / 31 / 32.25 / 31',
          '79.39 / 79.39 / 79.39 / 79.39 / 83.36 / 79.39')

Code

library(tidyverse)
vect %>% 
  str_split( ' / ') %>% 
  map(~.x[.x!=lag(.x) | is.na(lag(.x))]) %>% 
  map_chr(paste, collapse=' / ')

Output

[1] "27.82 / 23.65 / 27.82"   "19.87 / 19.14 / 19.87"   
[3] "32.25 / 31 / 32.25 / 31" "79.39 / 83.36 / 79.39"
Vlad C.
  • 944
  • 7
  • 12
0

Data: data<-data.frame(Des=c("a","b","c","d"),price=c('27.82 / 27.82 / 23.65 / 27.82', '19.87 / 19.87 / 19.14 / 19.87','32.25 / 32.25 / 31 / 32.25 / 32.25','79.39 / 79.39 / 79.39 / 79.39 / 83.36 / 79.39'))

  Des                                         price
1   a                 27.82 / 27.82 / 23.65 / 27.82
2   b                 19.87 / 19.87 / 19.14 / 19.87
3   c            32.25 / 32.25 / 31 / 32.25 / 31
4   d 79.39 / 79.39 / 79.39 / 79.39 / 83.36 / 79.39

Code:

data$`New Column`<-gsub("\\b([\\w\\.]+)( / \\1\\b)+","\\1",data$price,perl = T)

Outcome:

Des                                         price            New Column
1   a                 27.82 / 27.82 / 23.65 / 27.82   27.82 / 23.65 / 27.82
2   b                 19.87 / 19.87 / 19.14 / 19.87   19.87 / 19.14 / 19.87
3   c               32.25 / 32.25 / 31 / 32.25 / 31   32.25 / 31 / 32.25 / 31
4   d 79.39 / 79.39 / 79.39 / 79.39 / 83.36 / 79.39   79.39 / 83.36 / 79.39
harryjerry
  • 66
  • 3
  • Got it. However can we a new dimension to the data. For example, In new column we have 27.82 / 23.65 / 27.82, so can we add another column and tell, the price decreased and increased. I mean the initial value was 27.82, then it got decreased and then increased. Similarly for 32.25 / 31 / 32.25 / 31, here it is decreased, increased, decreased. –  Sep 13 '18 at 10:32
  • Hi I got the out put the problem is for 5 / 6 /16 / 5 it is taking as 5/16/2005 and for 2/1/1/1 it is taking as 43132. please suggest –  Sep 13 '18 at 19:33
  • because you skip the spaces – harryjerry Sep 14 '18 at 03:46
  • No in R the output is fine. But when i export to excel . it is converting as date –  Sep 14 '18 at 03:54
  • Can we change the special character to > instead of /. Then I think we don't get it. What I say –  Sep 14 '18 at 15:18