使用R查找行中的唯一位置

使用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

考虑以下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") , 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))))