使用R查找行中的唯一位置
考虑以下data.frame:使用R查找行中的唯一位置,r,dataframe,R,Dataframe,考虑以下data.frame: df <- data.frame(ID = 1:2, Location = c("Love, Love, Singapore, Love, Europe, United States, Japan, Amazon, Seattle, Orchard Road, Love", "Singapore, Singapore, Singapore") , stringsAsFactor
df <- data.frame(ID = 1:2, Location = c("Love, Love, Singapore, Love, Europe, United States, Japan, Amazon, Seattle, Orchard Road, Love",
"Singapore, Singapore, Singapore") , stringsAsFactors = FALSE)
df在base R中,我们可以在逗号上拆分字符串,并仅为每个位置粘贴唯一的字符串
df$unique.Location <- sapply(strsplit(df$Location, ","), function(x)
toString(unique(trimws(x))))
您可以结合使用strsplit
、sapply
和unique
:
df$Unique.Location <- sapply(strsplit(df$Location, split = ", "), function(x) paste0(unique(x), collapse = ", "))
df$Unique.Location使用tidyverse的选项
library(dplyr)
library(purrr)
df %>%
mutate(unique.Location = str_extract_all(Location, "\\w+") %>%
map_chr(~ toString(unique(.x))))
可能重复
df$Unique.Location <- sapply(strsplit(df$Location, split = ", "), function(x) paste0(unique(x), collapse = ", "))
library(dplyr)
library(purrr)
df %>%
mutate(unique.Location = str_extract_all(Location, "\\w+") %>%
map_chr(~ toString(unique(.x))))