[英]subsetting (querying) dataframe preserving order in R
I would like to know a way of subsetting with [] which preserves the order of the queries (authors), ex: 我想知道一种使用[]进行子设置的方法,该方法可以保留查询(作者)的顺序,例如:
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
You can use match
, ie 您可以使用
match
,即
authorcountry$countryalone[match(authors, authorcountry$authoralone)]
#[1] "U.S.A" "Argentina" "Argentina" "Brazil"
Or using dplyr
或使用
dplyr
library(dplyr)
authorcountry %>%
slice(match(authors, authoralone)) %>%
.$countryalone
#[1] "U.S.A" "Argentina" "Argentina" "Brazil"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.