将行号传递给另一列

时间:2014-09-30 00:44:30

标签: r tapply

我有一个数据框。

我需要在第1列中找到第2列的每个值的最小值。但是我应该从与第1列中找到的最小值相同的行返回第3列中的值。

第一部分似乎由tapply(1,2, min)

解决

但是如何将同一行传递给第3列?

更复杂的任务是当第一列中的最小值不唯一时。然后我需要按字母顺序选择第一个名称(多个),然后再次从第3列的同一行中找到相应的值。

3 个答案:

答案 0 :(得分:1)

一个可重复的例子可以很方便地完全理解你的问题。

但是,我认为你可以使用ave。

a<-c(1:10)
b<-c(rep(1,3),rep(2,4),rep(3,3))
c<-c(101:110)

df<-cbind(a,b,c)

给出了

df
      a b   c
[1,]  1 1 101
[2,]  2 1 102
[3,]  3 1 103
[4,]  4 2 104
[5,]  5 2 105
[6,]  6 2 106
[7,]  7 2 107
[8,]  8 3 108
[9,]  9 3 109
[10,] 10 3 110

所以我要找到我的b的最小值并保留相应的c。

rows<-df[which(ave(df[,1],df[,2],FUN=function(x) x==min(x))==1),]

给出了

rows
     a b   c
[1,] 1 1 101
[2,] 4 2 104
[3,] 8 3 108

答案 1 :(得分:1)

阅读评论后不清楚。

library(dplyr)
 df %>% 
    group_by(zone) %>%
    filter(population==min(population)) %>%
    #ungroup() %>% #if you don't need zone
    select(name)
 #    zone           name
 #  1    3 American-Samoa
 #  2    1        Andorra
 #  3    2         Angola

更新

 devtools::install_github("hadley/dplyr")
 devtools::install_github("hadley/lazyeval")

 library(dplyr)
 library(lazyeval)

 fun2 <- function(grp, Column, grpDontShow=TRUE){ 
         stopifnot(is.numeric(df[,grp]) & Column %in% colnames(df))
         df1 <- df %>% 
                   group_by_(grp) %>%
                   filter_(interp(~x==min(x), x=as.name(Column)))%>%
                   arrange(name) %>%
                   filter(row_number()==1) %>%
                   select(name)     
        if(grpDontShow){
                ungroup(df1) %>%
                          select(name)
                 }
        else {
            df1
          }            
        }       

 fun2("zone", "population", TRUE)
 # Source: local data frame [3 x 1]

 #            name
 #1        Andorra
 #2         Angola
 #3 American-Samoa

  fun2("zone", "landmass", FALSE)
  #Source: local data frame [3 x 2]
  #Groups: zone

  #  zone           name
  #1    1        Albania
  #2    2         Angola
  #3    3 American-Samoa

   fun2("ozone", "landmass", FALSE)
   #Error in `[.data.frame`(df, , grp) : undefined columns selected

  fun2("name", "landmass", FALSE)
  #Error: is.numeric(df[, grp]) & Column %in% colnames(df) is not TRUE

UPDATE2

如果您需要使用base R

的功能
  funBase <- function(grp, Column, grpDontShow = TRUE) {
            stopifnot(is.numeric(df[, grp]) & Column %in% colnames(df))
            v1 <- c(by(df[, c(Column, "name")], list(df[, grp]),
                   FUN = function(x) sort(x[,2][x[, 1] == min(x[, 1],
                                                   na.rm = TRUE)])[1]))

             if (grpDontShow) {
               data.frame(name = v1, stringsAsFactors = FALSE)
             }
              else {
             setNames(data.frame(as.numeric(names(v1)),
                       v1, stringsAsFactors = FALSE), c(grp, "name"))

            }
         }

   funBase("zone", "landmass")
   #            name
   #1        Albania
   #2         Angola
   #3 American-Samoa

  funBase("zone", "population", FALSE)
  #  zone           name
  #1    1        Andorra
  #2    2         Angola
  #3    3 American-Samoa

数据

 df <- structure(list(name = c("Afghanistan", "Albania", "Algeria", 
 "American-Samoa", "Andorra", "Angola"), landmass = c(5L, 3L, 
 4L, 6L, 3L, 4L), zone = c(1L, 1L, 1L, 3L, 1L, 2L), area = c(648L, 
 29L, 2388L, 0L, 0L, 1247L), population = c(16L, 3L, 20L, 0L, 
 0L, 7L)), .Names = c("name", "landmass", "zone", "area", "population"
 ), class = "data.frame", row.names = c("1", "2", "3", "4", "5", 
 "6"))

答案 2 :(得分:0)

尝试:

> ddf
    col1 col2 col3
 1:    5    a    A
 2:    2    a    B
 3:    3    a    C
 4:    6    a    D
 5:    4    b    E
 6:    2    b    F
 7:    6    b    G
 8:    2    b    H
 9:    7    c    I
10:    2    c    J
11:    6    c    K
12:    4    c    L
13:    2    c    M
> 
> sapply(split(ddf, ddf$col2), 
         function(x) {x = x[order(x$col3),]; x$col3[which.min(x$col1)]})
a b c 
B F J 
Levels: A B C D E F G H I J K L M

使用@ lynghonig的数据:

> sapply(split(ddf, ddf$b), 
         function(x) {x = x[order(x$c),]; x$c[which.min(x$a)]})
  1   2   3 
101 104 108 

使用OP的数据(来自评论):

> sapply(split(ddf, ddf$landmass), function(x) {x = x[order(x$zone),]; x$zone[which.min(x$name)]})
3 4 5 6 
1 1 1 3