0

I have a data which looks like this, it has NAplaces.

df<- structure(list(Proteins = c("A0A0J9YWL0;Q9Y4K1-2;Q9Y4K1", "H0YLZ8;P15144", 
"P08238", "P63261;P60709", "P10809", "A0A0G2JS76;A0A0G2JMX7", 
"A0A0G2JS76;A0A0G2JMX7;", "A0A087WYX8;Q14CM0", "B4DNK4;P14618-3;P14618;P14618-2;H3BR70", 
"A0A0G2JS76;A0A0G2JMX7", "A0A0G2JS76;A0A0G2JMX7", "P07737;K7EJ44"
), `data type BO_10min` = c(NA, "By whama", NA, "By whama", "By TM/T", 
"By TM/T", "By TM/T", NA, NA, "By TM/T", "By TM/T", NA), `data type BO1_1min` = c(NA, 
NA, "By TM/T", "By TM/T", "By TM/T", "By TM/T", "By TM/T", NA, 
"By TM/T", "By whama", "By TM/T", "By TM/T"), `data type BO1_20min` = c(NA, 
NA, NA, "By whama", NA, "By TM/T", "By whama", NA, NA, "By TM/T", 
"By TM/T", NA)), .Names = c("Proteins", "data type BO_10min", 
"data type BO1_1min", "data type BO1_20min"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -12L))

I am trying to get the output like this

H0YLZ8;P15144   data type BO_10min
P08238          data type BO1_1min
P63261;P60709   data type BO_10min
P63261;P60709   data type BO1_1min
P63261;P60709   data type BO1_20min
P10809          data type BO_10min
P10809          data type BO1_1min
.
.
.

It means if there is a string in a cell of a column corresponding to each row, it will paste the row again and paste the name of the column there

nik
  • 2,500
  • 5
  • 21
  • 48
  • @Jaap there is only two variables , here I have many variables – nik Nov 28 '17 at 15:54
  • If you have package `reshape2` installed, can you explain how to go from this `reshape2::melt(df, id.vars = "Proteins", measure.vars = names(df)[-1])` to your expected output? I am not seeing a rule there. – Rui Barradas Nov 28 '17 at 15:56
  • @Rui Barradas I like your solution , the only problem is that it paste also the `NA` if you could give me a solution, I would definitely accept and like it. Thanks – nik Nov 28 '17 at 15:59
  • @nik Did you look at the linked Q&A? Based on [my answer over there](https://stackoverflow.com/a/25856135/2204410) you need something like `gather(df, key, val, -1, na.rm = TRUE)` or `melt(df, id.var = 'Proteins', na.rm = TRUE)` – Jaap Nov 28 '17 at 15:59

0 Answers0