0
votes

Comment rechercher et extraire des mots appariés d'une colonne différente d'un Dataframe?

J'ai une variable dans un fichier de données qui a le nom de champ "Destination '. Ce champ contient des destinations / lieux (peut être pays, continent, plusieurs comtés, villes, ville, etc. ou les deux). J'ai un autre Dataframe qui contient 3 colonnes continent_name, Nom de Country_Name, City_Name, etc. Je veux obtenir une nouvelle colonne avec le continent, le pays, les noms de ville par le champ de destination correspondant avec 2 colonnes de Dataframe.

Dataframe A: P>

+---------+-----------------------+--------------------+----------------+
|  Name   |       Continent       |      Country       |      City      |
+---------+-----------------------+--------------------+----------------+
| Alex    | North America, Europe | France             |                |
| Mike    | NA                    | Germany, Australia | Boston, London |
| Charlie | Europe                | China, India       | New York       |
| Lophy   | Antartica, Europe     | UK                 | Delhi          |
+---------+-----------------------+--------------------+----------------+

r

0 commentaires

3 Réponses :


0
votes

Plusieurs fonctions qui vous aideront:

tolower () mettra tous vos mots à des minuscules afin que vous ayez des correspondances lorsqu'il y a un mélange de lettres majuscules. str_split () à partir de stringr vous permettra de séparer vos destinations par des éléments séparés par des virgules

Donc, tout d'abord, vous devez obtenir un vecteur de toutes vos destinations:

Destination_Vector <-unique (Unkist (Strsplit (Tolower (destination), ","))) fera. Parce que strpsplit vous donne une liste, vous avez besoin Unlist pour obtenir un vecteur. Unique obtiendra des doublons le cas échéant.

Ensuite, vous devez vérifier si l'une de vos destinations est dans le continent, le pays ou la ville:

Continent [Continent% in% Destination_Vector] fera. La même chose pour le pays et la ville

Ensuite, vous pouvez utiliser coller avec sep = "," pour rejoindre tout à l'aide de virgules comme séparateur.

meilleur!


0 commentaires

2
votes

Le plus facile consiste à mettre les deux tables en format long et à les rejoindre, puis revenez au format large à l'aide du type de destination:

A <-
  tribble(~Name   , ~Destination ,   
 'Alex'    , 'North America, Europe & France',     
 'Mike'    , 'Boston, London, Germany, Australia', 
 'Charlie' , 'China, Europe, India, New York', 
 'Lophy'   , 'Antartica, UK, Europe, Delhi')     


# anatartica typo corrected into antartica  
B <- tribble(~Continent, ~Country, ~City,
 'north america' , 'france'    , 'boston'   ,
 'antartica'    , 'germany'   , 'london'   ,
 'europe'        , 'australia' , 'delhi'    ,
 'XYZ'           , 'china'     , 'new york' ,
 'ABC'           , 'india'     , 'RST'      ,
 'PQR'           , 'UK'        , 'JKL')


0 commentaires

0
votes
# data
d <- read.table(text = "Name Destination
Alex 'North America, Europe & France'
Mike 'Boston, London, Germany, Australia'
Charlie 'China, Europe, India, New York'
Lophy 'Antartica, UK, Europe, Delhi'",
                header = TRUE,
                stringsAsFactors = FALSE)
d$Destination <- gsub("&", ",", d$Destination)
d$Destination <- tolower(d$Destination)
d$Destination <- trimws(d$Destination)
d

d2 <- read.table(text = " Continent  Country City
'north america' france boston
anatartica  germany london
europe australia delhi
XYZ china 'new york' 
ABC india RST
PQR UK  JKK", header = TRUE, stringsAsFactors = FALSE)
d2

# splits ..
check_fun <- function(a, b) {
  toString(intersect(trimws(strsplit(d$Destination[a], ",")[[1]], "both"), d2[[b]]))
}

want <- as.data.frame(do.call(cbind,
                              lapply(colnames(d2),
                                     function(x) {
                                       sapply(seq_along(d$Destination),
                                              function(y) {
                                                check_fun(y, x)
                                              }
                                              )
                                       })), stringsAsFactors = FALSE)
colnames(want) <- colnames(d2)
want$Name <- d$Name
want                              

# Continent            Country           City    Name
# 1 north america, europe             france                   Alex
# 2                       germany, australia boston, london    Mike
# 3                europe       china, india       new york Charlie
# 4                europe                             delhi   Lophy  

0 commentaires