2016-12-21 4 views
0

Я хотел бы знать способ Подменит с [], сохраняющим порядок запросов (авторы), например:Подменят (выполнение запроса) dataframe сохранения порядка в R

authors<-c("Almeda","Acosta", "Moscone", "Guerra") 

authorcountry <- read.table(text=" 
authoralone countryalone 
1  Acosta  Argentina 
2 Aguilera  Argentina 
3  Almeda   U.S.A 
4  Alves  Brazil 
5  Araújo  Brazil 
6 Bernardello  Argentina 
7  Daviña  Argentina 
8  Guerra  Brazil 
9  Honfi  Argentina 
10 Moscone  Argentina",header=TRUE,fill=TRUE,stringsAsFactors=FALSE) 

authorcountry$countryalone[(authorcountry$authoralone %in% authors)] 
#order should be: 
# [1] "U.S.A"  "Argentina" "Argentina" "Brazil" 

#as in: 
authors<-data.frame(authors) 
get_merge<-merge(authors,authorcountry, by.x="authors", by.y="authoralone", sort = FALSE) 
get_merge$countryalone 

ответ

3

Вы можете использовать match, т.е.

authorcountry$countryalone[match(authors, authorcountry$authoralone)] 
#[1] "U.S.A"  "Argentina" "Argentina" "Brazil" 
1

Или с помощью dplyr

library(dplyr) 
authorcountry %>% 
     slice(match(authors, authoralone)) %>% 
     .$countryalone 
#[1] "U.S.A"  "Argentina" "Argentina" "Brazil" 
Смежные вопросы