-2

我有一个数据框。

我需要为第二列的每个值在第一列中找到一个最小值。但是我应该从与第一列中找到的最小值相同的行返回第三列中的值。

第一部分似乎解决了tapply(1,2, min)

但是如何将同一行传递到第三列?

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

4

3 回答 3

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

所以我要找到 a 我的 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
于 2014-09-30T01:29:27.840 回答
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

更新2

如果您需要使用功能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"))
于 2014-09-30T05:01:34.900 回答
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 
于 2014-09-30T01:52:53.520 回答