2016-09-04 8 views
1

私は、データフレームの要素を置き換えるより効率的なバージョンのコードを探しています。ルックアップテーブルを使用して因子値を置換する

structure(list(Rio.Olympics.Sports.Participating.Team = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("American Gymnastics", 
"American Swimmers", "Boxing", "European Gymnastics", "Running", 
"Free-style swimming", "Breaststroke Swimming", "Diving", "Athletics", 
"Soccer"), class = "factor"), Calendar.Quarter = structure(c(16071, 
16161, 16252, 16344, 16436, 16526, 16617, 16709, 16801, 16892, 
16983, 17075, 16071, 16161, 16252, 16344, 16436, 16526, 16617, 
16709, 16801, 16892, 16983, 17075, 16071, 16161, 16252, 16344, 
16436, 16526, 16617, 16709, 16801, 16892, 16983, 17075, 16071, 
16161, 16252, 16344, 16436, 16526, 16617, 16709, 16801, 16892, 
16983, 17075, 16071, 16161, 16252, 16344, 16436, 16526, 16617, 
16709, 16801, 16892, 16983, 17075), class = "Date"), Randomized.Viewers = c(49, 
45, 51, 55, 47, 48, 54, 57, 53, 50, 52, 58, 32, 29, 33, 40, 34, 
36, 31, 39, 37, 30, 35, 41, 5, 1, 25, 46, 38, 4, 56, 27, 21, 
43, 42, 44, 2, 59, 3, 10, 60, 7, 14, 24, 13, 16, 17, 28, 15, 
6, 19, 23, 11, 12, 20, 22, 9, 8, 18, 26)), .Names = c("Rio.Olympics.Sports.Participating.Team", 
"Calendar.Quarter", "Randomized.Viewers"), row.names = c(NA, 
-60L), class = "data.frame") 

は今、私が要因のラベルを変更したい:

は、ここに私のデータセットです。ここに私がやったことだ:

Old_labels<- c("American Swimmers","American Gymnastics","European Gymnastics","Running","Boxing") 
New_labels<- c("Jupitean Swimmers","Saturnish Gymastics","Plutoish Gymnastics","Walking","Fighting") 
Apply_lables<-data.frame(Old_labels,New_labels) 
colnames(Apply_lables)[1]<-"Old_labels" 

最後に、このコードは、トリックを行うだろう:Rで初心者として、:

Rio.Olympics.Sports.Participating.Team Calendar.Quarter Randomized.Viewers 
1      Jupitean Swimmers  2014-01-01     49 
2      Jupitean Swimmers  2014-04-01     45 
3      Jupitean Swimmers  2014-07-01     51 
4      Jupitean Swimmers  2014-10-01     55 
5      Jupitean Swimmers  2015-01-01     47 
6      Jupitean Swimmers  2015-04-01     48 

質問:

p1<-p 
p1$Rio.Olympics.Sports.Participating.Team<-Apply_lables[match(p$Rio.Olympics.Sports.Participating.Team,Apply_lables$Old_labels),"New_labels"] 

ここで修正データフレームです私は数時間苦労しました。ルックアップテーブルに基づいて要素を変更するには、より良い方法(コードの行数を減らし、実装を高速化する方法)がありますか?オリジナルのデータセットは約1M行あり、上記のコードは実行に多くの時間がかかります。

私はこのトピックをSOで研究しましたが、これはどこにも含まれていなかったと思います。ルックアップテーブルを使って行を変更するmatch()の使い方について述べた記事がいくつかありましたが、

+1

CRANを打つ新しい 'forcats'パッケージをチェックしてください今週。 – hrbrmstr

+0

あるいは 'levels(df $ var)[match(Old_labels、levels(df $ var))] < - New_labels'を使って新しい依存関係を完全に追加しないでください。 – thelatemail

答えて

0

あなたが特定のリストからOld_labelsを交換したい場合は、私はあなただけで希望の要因のために、あなたのlevels()をサブセットし、そこに新しいラベルをプッシュすることができ推測:

levels(dta$Rio.Olympics.Sports.Participating.Team)[ 
    dta$Rio.Olympics.Sports.Participating.Team %in% Old_labels] <- 
    New_labels 
関連する問題