-2

I am comparing two columns and I do want to extract only characters that were added on previous column values. I only want to compare differences and extract added characters to previous value on row..Look at this table and see how expected output on diff column should look like.

dput(df)
structure(list(v1 = c("John|Alice,Mark|mercy, Austin|Silva", "Eunice|stoney, Brandon|Mary", "Apple| -Mango"),
               v2 = c("John|Alice,Mark|mercy, Austin|Silva|James |Jacy",  "NA ", "Apple| +Mango | Orange"),
               diff = c("|James |Jacy","NA", "+ |Orange")),
              class = "data.frame", row.names = c(NA,  -3L))

I have tried this code but it gives me the whole values in column1 and column2 but I want it to give the newly added characters to the previous one

library(dplyr); library(stringr)
dff <- df %>% mutate(diff = str_remove(v1,v2))
  • @akrun really desperate for this solution bro – Livingstone Jul 24 '19 at 14:14
  • 1
    I would suggest that you edit your question to narrow it down to a single language and add current and expected output in a copy-pastable manner. It's not about the number of times you ask a question, once is enough if done right. To achieve the above, please see [this post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – NelsonGon Jul 24 '19 at 14:30
  • I have edited and added a reproducible example – Livingstone Jul 25 '19 at 08:01

1 Answers1

1

You just need to specify the correct delimiters to split from,

 Map(function(x, y) paste(setdiff(y, x), collapse = '| '), strsplit(df$v1, '\\||, | | -| \\+'), strsplit(df$v2, '\\||, | | -| \\+'))

#[[1]]
#[1] "James| | Jacy"

#[[2]]
#[1] "NA"

#[[3]]
#[1] "Orange"

To assign back to the data frame, it is better to use mapply and simply assign, i.e.

df$diff1 <- mapply(function(x, y) paste(setdiff(y, x), collapse = '| '), strsplit(df$v1, '\\||, | | -| \\+'), strsplit(df$v2, '\\||, | | -| \\+'))
Sotos
  • 51,121
  • 6
  • 32
  • 66
  • how can I add the results of this code to a data frame row where it was found. seems I can't write a vector into CSV if I try the data gets messy – Livingstone Jul 25 '19 at 12:57
  • edited my answer. Let me know If you need anything else – Sotos Jul 25 '19 at 13:00
  • Hey man, so need some assistance. I have some rows in my data frame with a separator, How can select columns which have those rows with specific separator – Livingstone Jul 31 '19 at 09:03
  • You can use `grepl(delimiter, df_column)`. If you share an example I can help you further – Sotos Jul 31 '19 at 09:14
  • Is it possible I loop through the entire dataframe select only those columns with that delimiter instead of specifying a given column – Livingstone Jul 31 '19 at 09:17
  • check out this question https://stackoverflow.com/questions/57184018/filter-columns-with-specific-separator-in-rows – Livingstone Jul 31 '19 at 09:22
  • Sure. Something like `df[grepl('your_delimiter', df)]` will only return the columns with `your_delimiter` – Sotos Jul 31 '19 at 09:23
  • wait a minute, it does not filter rows, its filterring columns with that delimiter but some row ae coming completely. say first row had the delimiter at column 5, I am still getting column 1 up to 4 with rows yet I just want column 5 and row value that has delimiter – Livingstone Jul 31 '19 at 09:52
  • Hey man, so if I have a dataframe with some rows having a formula but that formula also has a statement with an "and" string. How can I find rows which only have an "and" string. also say can I find rows with cells having duplicated string, example is a cell that is like this [JOHN = '1' and JOHN ='1'] Can I be able to capture cells looking like this? – Livingstone Aug 05 '19 at 07:18
  • Hey. You keep changing the question. I suggest that you revise your data/methodology, decide what you need to do and then post a question. What you are doing right now is a waste of time for both of us. – Sotos Aug 05 '19 at 07:36
  • Hey man sorry, Its because I have been bared from asking questions on stack overflow. So thought I should ask here but Thank you – Livingstone Aug 05 '19 at 07:48