Select nth element in data frame by factor

前端 未结 3 1766
迷失自我
迷失自我 2021-01-21 22:29

I\'ve got a dataframe with a text column name and factor city. It is ordered alphabetically firstly by city and then name. No

3条回答
  •  不知归路
    2021-01-21 22:56

    In base R using by:

    Set up some test data, including an additional out of range value:

    test <- read.table(text="name    city
    John    Atlanta
    Josh    Atlanta
    Matt    Atlanta
    Bob     Boston
    Kate    Boston
    Lily    Boston
    Matt    Boston
    Bob     Seattle
    Kate    Seattle",header=TRUE)
    

    Get the 3rd item in each city:

    do.call(rbind,by(test,test$city,function(x) x[3,]))
    

    Result:

            name    city
    Atlanta Matt Atlanta
    Boston  Lily  Boston
    Seattle     
    

    To get exactly what you want, here is a little function:

    nthrow <- function(dset,splitvar,n) {
        result <- do.call(rbind,by(dset,dset[splitvar],function(x) x[n,]))
        result[,splitvar][is.na(result[,splitvar])] <- row.names(result)[is.na(result[,splitvar])]
        row.names(result) <- NULL
        return(result)
    }
    

    Call it like:

    nthrow(test,"city",3)
    

    Result:

      name    city
    1 Matt Atlanta
    2 Lily  Boston
    3  Seattle
    

提交回复
热议问题