基于部分字符串匹配的子集数据帧

问题描述 投票:0回答:1

我有一个数据框,其中包含大学名称和各部门,中心和机构的名称。我想提取包含字符串“University”的所有单元格并将其另存为矢量。

我已经尝试过grep函数但是因为我对R很新,所以我没有设法在数据框的多个列上编写正确的函数。

这是我的例子:

 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))

预期结果:

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

数据框具有或多或少随机分散的大学名称,我想将其提取到单个向量中。由于我对特定大学的出现次数感兴趣,因此需要在向量中重复名称。

r string dataframe grep subset
1个回答
1
投票

我们可以unlist为`大学'的data.frame和grep

out <- data.Frame(Universities = grep("University", unlist(df), 
         ignore.case = TRIE.  value = TRUE))
© www.soinside.com 2019 - 2024. All rights reserved.