【问题标题】:get a subset of a dataframe by sampling for each value in a column通过对列中的每个值进行采样来获取数据帧的子集
【发布时间】:2013-06-23 22:27:22
【问题描述】:

我有一个dataframe,其中所有行都有一个值uid,对应于一个用户ID,并且多行可以有相同的uid。我想为每个 uid 创建一个仅包含 x 行的随机样本的新数据框。

我写了这个函数:

trim <- function(df, max){
    data.by.user <- split(df, df$uid) #split the dataframe by user
    output <- NULL
    lapply(data.by.user, function(x){

        #length(x$tid) = number of rows for that user

        if(is.null(output){
            if(length(x$tid) <= max){
                output <<- x
            }
            }else{
                output <<- x[sample(nrow(x), size = max),]
            }
        }else if (length(x$tid) <= max){
            output <<- rbind(output, x)
        }else{
            output <<- rbind(output, x[sample(nrow(x), size=max),]) #sample 'max' rows from x
        }
    })
    return(output)
}

但是当我在我的数据框(有几百万行)上尝试它时,

d <- trim(old_df, 200)

它耗尽内存并收到此错误以及有关已达到内存总分配的警告:

Error: cannot allocate vector of size 442 Kb

有没有更节省内存的方法来实现这一点?

【问题讨论】:

    标签: r memory split dataframe


    【解决方案1】:

    我会尽可能避免使用数据框(子集)。您正在拆分df,您实际上只需要使用行索引。此外,您正在创建一个列表并反复增长它,这会占用内存。

    这是一个精简版。我不知道您的数据集的详细信息,但在 100 万行 x 2 列的数据帧上测试它需要几秒钟。

    samp <- function(df, size=100, replace=FALSE)
    {
        grp <- split(seq_len(nrow(df)), df$id)
        l <- lapply(grp, function(g) {
            if(length(g) < size && !replace)
                g
            else sample(g, size=size, replace=replace)
        })
        df[unlist(l), ]
    }
    
    df <- data.frame(x=seq(1e6), id=sample(1000, 1e6, replace=TRUE))
    df2 <- samp(df)
    dim(df2)
    [1] 100000      2
    

    【讨论】:

    • 这似乎没有考虑 id 的行数少于“size”行的情况
    • 编辑考虑到这一点。
    • 谢谢!用你原来的函数做“df2
    • 我知道replace=FALSE 没有任何问题。该选项已经存在了很长时间,以至于现在应该已经解决了所有问题。
    • 好的,知道了。非常感谢你的帮忙!你的功能在速度和内存上都比我的好很多
    【解决方案2】:

    如果您想改变不同层的样本大小(在您的情况下为“uid”列,在我的示例中为“cyl”列),并且如果您有一个大小如下的数据框:

    sizes <- data.frame(cyl=c(4,6,8), size=c(2,333,4))
    
    > sizes
      cyl size
    1   4    2
    2   6  333
    3   8    4
    

    然后你可以使用这个调用来做你的样本,它会忽略大于每个层的行数的样本大小:

    Reduce(rbind, by(mtcars, mtcars$cyl, function(d)
           d[sample(nrow(d), min(with(sizes, size[cyl==unique(d$cyl)]), nrow(d))),]))
    

    结果:

                         mpg cyl  disp  hp drat    wt  qsec vs am gear carb
    Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
    Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
    Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
    Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
    Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
    Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
    Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
    Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
    Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
    Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
    Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
    Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
    Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      • 2017-08-20
      • 2015-01-31
      • 1970-01-01
      • 2020-11-10
      • 2015-04-19
      • 2022-10-01
      相关资源
      最近更新 更多