-1

Suppose I have data-frame x. It has column "A" and "B".

A        B
a b      a b
c d      c d
e f      e f
a b      a b
a b
a b      a b
a b      a b
a b
a b
a b
a b

I want to display something like this.

A        B
a b      a b
c d      c d
e f      e f
a b      a b
a b      a b
a b      a b
a b      a b
a b      a b
a b      a b 
a b      a b
a b      a b

Basically I want to do filter on x.A for value "a b", so that I can get exact same value on x.B. For column B I have only "a b" and " " values for that. I want to convert " " to "a b" in B column.

I have tried using gsub, sub, filter with regex but after cleaning data if I export this .csv file in excel I am not getting correct output what I want.

camille
  • 16,432
  • 18
  • 38
  • 60
  • It's helpful to see what you've tried even if it hasn't worked. Are you just trying to subset for when the values in A and B are the same? I'm also not sure what exporting to csv would have to do with the issue – camille May 14 '19 at 16:23
  • aren't you just doing `df$B = df$A` ?? – boski May 14 '19 at 16:24
  • No, because B and A has some different values too. – user11499525 May 14 '19 at 16:26
  • I want to fill value of blanks in B when A has a value "a b". And want to fill with "a b". Its a text. – user11499525 May 14 '19 at 16:28
  • @camille I want to fill the value for B with "a b" when A has a value "a b". – user11499525 May 14 '19 at 16:39
  • @divibisan library(plyr) junk$nm <- revalue(junk$nm, c("B"="b")) ... this can work but I want filter for "A" column. because "A" has multiple values but I want to do that for particular one only. – user11499525 May 14 '19 at 17:56
  • @divibisan , I want to something like this: if A == a b then B == a b. – user11499525 May 14 '19 at 18:25
  • Have you looked at the accepted answer on the linked question? You need to change the names of the column that is being checked in the conditional and the column that is being changed – divibisan May 14 '19 at 19:05
  • I have to use condition here. If I have to do in Excel it's simple. I want filter on "A" with value "a b". then I will get all "a b" from "A". and for "B" side I will get either "a b" or " ". Instead of " " I want "a b" on "B" side. – user11499525 May 14 '19 at 19:15
  • But I want this to automate in R. – user11499525 May 14 '19 at 19:17

1 Answers1

0

It seems you want to check whether column B has any value. If yes, keep this, if not, put the value of column A there, you can use ifelse:

df$B <- ifelse(df$B == "", df$A, df$B)
Sven
  • 1,203
  • 1
  • 5
  • 14