首页 > 解决方案 > 无法分配向量 - lapply 函数

问题描述

使用这个函数,我从整个 df 中减去每一列。现在df太大了,我总是收到错误消息:无法分配大小的向量。是否有可能将任务拆分为不同的部分以获得相同的结果?

或者知道我能做什么?df 约为 2500 列。

感谢您的任何建议或帮助!!

CX <- lapply(df, function(x) df - x)

更新:

library("dplyr")
library("tidyverse")
library("dlookr")
library("plyr")
library("readxl")
library("qpcR")
library("ggplot2")
library("matrixStats")



ZZ <- data.frame( Date = c("05-01-2001", "05-02-2001", "05-03-2001", "05-04-2001", "05-06-2001"),
                  Paris = c(25.3, 25.4, 26, 26.1, 25.6),
                  Berlin = c(18.5, 18.1, 19, 20.1, 23.9),
                  Tokyo = c(23.6, 23.9, 26, 26.5, 27),
                  Funchal = c(33.7, 32, 32.1, 31.4, 30.5),
                  Penaforte = c(26.1, 26, 26, 26.4, 26.1))


memory.limit(9999999999999) # 

ZZ500 <- select_if(ZZ, is.numeric)

getDiff <- function(ZZ, idx) {
  
  #This is the function to calculate all the temperature diffs
  
  num.rows <- nrow(ZZ)
  
  output.range <- 1:(num.rows-1)
  
  
  
  diff1  <- ZZ[output.range,1:ncol(ZZ)]
  diff2 <- ZZ[output.range+idx,1:ncol(ZZ)]
  
  diff3 <- data.frame(((diff1) / (diff2) -1))
  
  
  
  return(diff3)
  
}

out <- lapply(1:4, function(x) getDiff(ZZ500, x)) # calculates all diffs over a several period (here 1 to 4 days)

All_Diffs <- as.data.frame(out) 

All_Diffs_V1 <- All_Diffs

for(i in 1:ncol(All_Diffs_V1)){ #Negative changes = 0, Positive changes = 1
  
  All_Diffs_V1[ ,i] <- ifelse (All_Diffs_V1[ ,i] > 0,  1, 0)
  
}

df <- All_Diffs_V1

result <- lapply(df, function(x) df - x) # Subtract all the columns, possible results: 1,0,-1
result.df <- data.frame(result)

最后一部分不包括在内。在这一部分中,我试图找出一个例子:每年 5 月 16 日有多少次巴黎和东京之间的温度是负值或正值,以及每一列对、每一天和每个时间段的温度。

标签: r

解决方案


推荐阅读