0

我有全名数据,我使用 strsplit() 来获取名称的每个元素。

# Dataframe with a `names` column (complete names)
df <- data.frame(
    names =
          c("Adam, R, Goldberg, MALS, MBA", 
          "Adam, R, Goldberg, MEd", 
          "Adam, S, Metsch, MBA", 
          "Alan, Haas, MSW", 
          "Alexandra, Dumas, Rhodes, MA", 
          "Alexandra, Ruttenberg, PhD, MBA"),
    stringsAsFactors=FALSE)

# Add a column with the split names (it is actually a list)
df$splitnames <- strsplit(df$names, ', ')

我也有以下学位列表

degrees<-c("EdS","DEd","MEd","JD","MS","MA","PhD","MSPH","MSW","MSSA","MBA",
           "MALS","Esq","MSEd","MFA","MPA","EdM","BSEd")

我想得到每个名字和各自学位的交集。

我不确定如何展平名称列表,以便可以使用 intersect 比较两个向量。当我尝试unlist(df$splitname,recursive=F)时,它分别返回每个元素。任何帮助表示赞赏。

4

2 回答 2

3

尝试

df$intersect <- lapply(X=df$splitname, FUN=intersect, y=degrees)

df$splitname这将为您提供(例如 intersect(df$splitname[[1]], degrees))中每个元素的交集列表。如果你想要它作为一个向量:

sapply(X=df$intersect, FUN=paste, collapse=', ')

我假设您需要它作为向量,因为可能完整的名称来自一个(例如,来自数据框),但 strsplit 输出一个列表。

那样有用吗?如果不是,请尝试澄清您的意图。

祝你好运!

于 2013-02-20T04:31:52.533 回答
0

为了连续性,您可以使用unlist

hh <- unlist(df$splitname)
intersect(hh,degrees)

例如 :

ll <- list(c("Adam" ,    "R"    ,    "Goldberg" ,"MALS"  , "MBA "),
           c("Adam" ,    "R"    ,    "Goldberg", "MEd" ))

 intersect(hh,degrees)
[1] "MEd"

或相当于:

hh[hh %in% degrees]
[1] "MEd"

要获得差异,您可以使用

setdiff(hh,degrees)
[1] "Adam"     "R"        "Goldberg" "MALS"     "MBA "    

...

于 2013-02-20T04:42:07.707 回答