[英]How to extract multi-word units from a column using R?
我的數據是這樣的:
話題 | 措施 |
---|---|
氣候變化 | 減少排放 |
大流行 | 接種疫苗 |
慈善機構 | 呼吁捐款 |
現在我想提取一列中的所有多字單元(MWU),即:
topic_mwu<-c("氣候變化")
measure_mwu<-c("減少排放","呼吁捐款")
R中是否有自動提取這些MWU的功能? 基本上我只需要識別那些至少包括一個空格的條目,所以我正在考慮一個 RegEx - hack..
我非常感謝您的幫助!
下面的代碼應該工作:
#your dataframe
dt <- matrix(c("reduce emission", "call for donations", "pandemic", "climate change", "donations", "charity"), ncol =2)
#make it a vector
dt <- as.vector(dt)
#if the table is very big, you can do unique() to remove duplicates
dt <- unique(dt)
#get the MWU
dt[unlist(lapply(strsplit(dt,split = " "), length)) > 1]
這就是你要找的嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.