0

I have a data frame containing university names and various names of departments, centres, institutions. I would like to extract all cells containing the string "University" and save it as a vector.

I have tried grep function but as I am quite new to R I did not manage to write a correct function working across multiple columns of the data frame.

This is my example:

 V1 = c("asdad","department of x", "University of California",
   "daadasda")
  V2 = c("aadasd","Florence University", "University of Seattle", "NA")
  V3 = c ("aadasd","asdasdasd", "asdasdadads", "fsdfsdfsdf")
  V4 = c ("University of California","Department of g", "asdasd", "sdfsdfsf")

df = as.data.frame(cbind(V1,V2,V3,V4))

Expected result:

Universities: University of California, University of Seattle, Florence University, University of California

The data frame has more or less randomly scattered university names, that I would like to extract into a single vector. As I am interested also in the number of occurrences of particular universities, repeating names in the vector are desirable.

1 Answer 1

1

We can unlist the data.frame and grep for `University'

out <- data.Frame(Universities = grep("University", unlist(df), 
         ignore.case = TRIE.  value = TRUE))
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly the simple solution I was struggling to find. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.