2017-11-13 1 views
0

ich einen Datenrahmen haben sagen prod_score:Beobachtungen im Datenrahmen nach anderen Datenrahmen aktualisieren?

product score 
a  1 
d  2 
ff  2 
e  3 
fvf  1 

Ich habe eine andere Datenrahmen prod_rank mit gleichen Produkte + ihren Rang prod_rank:

product rank 
a   11 
d   4 
ff  1 
e   5 
fvf  9 

Nur um zu klären ich viele Beobachtungen haben, deshalb habe ich Probe zeigen Daten.

Filtern alle Produkte mit Score 2:

library(dplyr) 
prod_scr_2 <- prod_score %>% filter(score == 2) 

Jetzt möchte ich prod_scr_2 Produkte und aktualisieren Sie die Punktzahl nach dem prod_rank df nehmen:

ich verwendet habe beitreten:

decision_tbl <- inner_join(prod_scr_2, prod_rank, by = "product") %>% 
               top_n(2,desc(rank)) 

Jetzt nehme ich decision_tbl$product und möchte nur die Produkte aktualisieren, die den höchsten Rang bekommen.

I Spiel, dies zu tun verwendet haben:

prods2update_idx <- match(decision_tbl$product, prod_score$product) 

Gegeben Spiel Indizes Ich versuche, die prod_score Datenrahmen zu aktualisieren, raten Sie bitte, wie ich dies tun kann?

+1

So In diesem Fall, welche Produkte haben den höchsten Rang? Ist es nur Produkt "d", da es Rang 4 hat, wenn "ff" Rang 1 hat? – AntoniosK

+0

Je niedriger der Rang, desto besser. –

Antwort

1

Angenommen, die Punktzahl von Interesse ist 2 (wie Sie in Ihrem Beispiel erwähnt haben) und die aktualisierte Punktzahl für die Produkte mit dem höchsten Rang ist 100. Diese können geändert werden.

Hier ist eine dplyr Lösung, weil ich sah, Sie zu arbeiten begann, dieses Paket mit:

library(dplyr) 

prod_score = read.table(text = " 
product score 
a  1 
d  2 
ff  2 
e  3 
fvf  1 
", header = T, stringsAsFactors = F) 

prod_rank = read.table(text = " 
product rank 
a   11 
d   4 
ff  1 
e   5 
fvf  9 
", header = T, stringsAsFactors = F) 


prod_score %>% 
    filter(score == 2) %>%         # select products with score = 2 
    inner_join(prod_rank, by = "product") %>%    # join to get ranks 
    filter(rank == max(rank)) %>%       # keep product(s) with maximum ranks 
    rename(given_score = score) %>%      # change column name (for the next join) 
    right_join(prod_score, by = "product") %>%    # join to get scores 
    mutate(score = ifelse(!is.na(rank), 100, score)) %>% # update score when there's a rank value 
    select(-given_score, -rank)       # remove unnecessary columns 

# product score 
# 1  a  1 
# 2  d 100 
# 3  ff  2 
# 4  e  3 
# 5  fvf  1 

Und einen alternativen Ansatz in Basis R. Denken Sie daran, erstes Beispiel Datensätze neu zu bauen:

# get products with score = 2 
prod_score$product[prod_score$score == 2] -> prds_score_2 

# get ranks for those products 
prod_rank[prod_rank$product %in% prds_score_2,] -> prds_score_2_ranks 

# keep products with maximum rank to update 
prds_score_2_ranks$product[prds_score_2_ranks$rank == max(prds_score_2_ranks$rank)] -> prds_to_update 

# update values for those products in your initial table 
prod_score$score[prod_score$product %in% prds_to_update] = 100 

# see the updates 
prod_score 

# product score 
# 1  a  1 
# 2  d 100 
# 3  ff  2 
# 4  e  3 
# 5  fvf  1 
+0

Vielen Dank, die obige Lösung mit dplyr ist fast genau die, die ich verwendet habe :) –

Verwandte Themen