-1

I am trying to subset a data frame containing 626 obs. of 149 variables and I want to look for a specific string and return the rows that have that value regardless of what column it is found in.

For example:

I am looking for this string "GO:0004674" in a data frame that can contain this string in many different columns and rows as shown below in the image link.

enter image description here

For example the string "GO:0004674" can be found in row 12, 13 and 14. So I would want to keep only those rows and later on export them.

How can I perform this? All examples that I have seen thus far only look for string in a specific column and not in the whole dataframe.

Ant help will be greatly appreciated.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360

1 Answers1

0

You can use apply to do row-wise operation using the argument MARGIN = 1. Example:

mydf[apply(mydf, MARGIN = 1, FUN = function(x) {"GO:0004674" %in% x}), ]
Zach
  • 1,103
  • 8
  • 11
  • This returns only a subset of rows from my data frame, 6 to be exact. When I know that this particular string is found in 57 rows in my data frame. This is where i have been stuck on for a while now. I do not understand why when – Diego Almanza Oct 17 '16 at 20:18
  • Worked fine for me when I reconstructed your sample data - in any case @HubertL's answer worked for you. – Zach Oct 17 '16 at 20:25