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 | +---------+-----------------------+--------------------+----------------+
3 Réponses :
Plusieurs fonctions qui vous aideront: P>
Donc, tout d'abord, vous devez obtenir un vecteur de toutes vos destinations: p>
Ensuite, vous devez vérifier si l'une de vos destinations est dans le continent, le pays ou la ville: P>
Ensuite, vous pouvez utiliser meilleur! p> tolower () code> mettra tous vos mots à des minuscules afin que vous ayez des correspondances lorsqu'il y a un mélange de lettres majuscules.
str_split () code> à partir de stringr code> vous permettra de séparer vos destinations par des éléments séparés par des virgules p>
Destination_Vector <-unique (Unkist (Strsplit (Tolower (destination), ","))) code> fera. Parce que strpsplit code> vous donne une liste, vous avez besoin Unlist code> pour obtenir un vecteur. Unique code> obtiendra des doublons le cas échéant. P>
Continent [Continent% in% Destination_Vector] code> fera. La même chose pour le pays et la ville p>
coller code> avec sep = "," code> pour rejoindre tout à l'aide de virgules comme séparateur. P>
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')
# 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