1

何千もの異なる場所 (都市) 名を持つ列を持つ大きなデータフレームがあり、それを単純化/クリーンアップする必要があります。

かなり戦って正規表現とループでそれをやろうとした後、私は DataCombine パッケージと FindReplace を見つけました。

ので、私は持っています:

   UserId          Location
1   USR_1             Paris
2   USR_2            London
3   USR_3           Londres
4   USR_4           Neuilly
5   USR_5            Berlin
6   USR_6    London Chelsea
7   USR_7 Berlin Schoenfeld
8   USR_8          Paris-20
9   USR_9           Neuilly
10 USR_10     Friedrischain

たとえば、"London Chelsea" は "London"、"Brooklyn" は "New York City"、"Paris 20e"、"Paris-14" は "Paris" となります。さらに言えば、「Paris」というパターンを持つすべてのものを「Paris」に置き換えたいと思います (SQL の LIKE "Paris%" のようなものです)。

# Data for testing
library(DataCombine)
user_test <- data_frame(x <- paste("USR", as.character(1:10), sep = "_"), y <- c("Paris", "London", "Londres", "Neuilly", " Berlin", "London Chelsea", "Berlin Schoenfeld", "Paris-20", "Neuilly", "Friedrischain"))
colnames(user_test) <- c("UserId","Location")
user_test <- as.data.frame(user_test) ### Not sure why I have to put it there but otherwise it doesn't have the dataframe class
should_be <- data_frame(c("Paris", "London", "Berlin", "Neuilly", "Friedr"), c("Paris", "London", "Berlin", "Paris", "Berlin"))
colnames(should_be) <- c("is","should_be")

# Calling the function
FindReplace(data = user_test, Var = "Location", replaceData = should_be, from = "is", to = "should_be", exact = FALSE, vector = FALSE)

そして、関数はこれを返します:

   UserId          Location
1   USR_1             Paris
2   USR_2            London
3   USR_3           Londres
4   USR_4             Paris
5   USR_5            Berlin
6   USR_6    London Chelsea
7   USR_7 Berlin Schoenfeld
8   USR_8          Paris-20
9   USR_9             Paris
10 USR_10     Berlinischain

エントリ全体ではなく、部分的に消去されています (文字列は置き換えられています)。

どうすればできるかについてのアイデアはありますか?grep でループしますか? マッチ?それとも、必要なすべてのエントリを含むクリーニングデータフレームを本当に構築する必要がありますか?

4

1 に答える 1