小编maj*_*nik的帖子

R - 使用变量对重新组织熔化的data.frame

对不起,我在帖子标题中并不是特别明确.我希望我的例子会更清楚!

如果我从数据框开始:

test.df <- data.frame(group=c(rep("a",4), rep("b",4)), 
                  var=rep(1:4,2),
                  min= runif(8),
                  q25=runif(8,1,2),
                  q75=runif(8,2,3),
                  max=runif(8,3,4))
head(test.df,2)
  group var        min      q25      q75      max
1     a   1 0.59078504 1.199138 2.119283 3.869486
2     a   2 0.06131107 1.676109 2.603068 3.739955
Run Code Online (Sandbox Code Playgroud)

我知道可以用id = c(group,var)来融化它

library(reshape2)
head(melt(test.df, id=c("group", "var")),2)
  group var variable      value
1     a   1      min 0.59078504
2     a   2      min 0.06131107
Run Code Online (Sandbox Code Playgroud)

但我正在寻找的方法是通过配对min-max和q25-q75获得两个"值"列,以便它看起来像:

  group var variable  value1     value2  
1     a   1 min-max   0.59078504 3.869486
1     a   1 q25-q75   1.199138   2.119283
2     a   2 min-max   0.06131107 3.739955
2     a …
Run Code Online (Sandbox Code Playgroud)

r dataframe reshape2

7
推荐指数
1
解决办法
204
查看次数

高效(记忆)功能,用于重复距离矩阵计算和超大距离矩阵的分块

我想知道是否有人可以查看以下代码和最小示例并提出改进建议 - 特别是关于使用非常大的数据集时代码的效率.

该函数采用data.frame并将其按分组变量(因子)拆分,然后计算每个组中所有行的距离矩阵.

我不需要保持距离矩阵 - 只有一些统计数据,即均值,直方图......,然后它们可以被丢弃.

我对内存分配等知之甚少,我想知道最好的方法是什么,因为我将使用每组10.000-100,000个案例.任何想法将不胜感激!

此外,在遇到严重的内存问题时,将bigmemory或其他大型数据处理包包含在函数中的最不痛苦的方法是什么?

FactorDistances <- function(df) {
  # df is the data frame where the first column is the grouping variable. 
  # find names and number of groups in df (in the example there are three:(2,3,4)
  factor.names <- unique(df[1])
  n.factors <-length(unique(df$factor))
  # split df by factor into list - each subset dataframe is one list element
  df.l<-list()
  for (f in 1:n.factors) {df.l[[f]]<-df[which(df$factor==factor.names[f,]),]}
  # use lapply to go through list and calculate distance matrix for …
Run Code Online (Sandbox Code Playgroud)

memory-management r distance matrix chunking

3
推荐指数
1
解决办法
3433
查看次数

标签 统计

r ×2

chunking ×1

dataframe ×1

distance ×1

matrix ×1

memory-management ×1

reshape2 ×1