我有一個功能,我用兩種不同的方式在相同的數據上執行。但是,結果並不相同。我的問題是我如何解決這個問題。我構建了一個數據框(使用read.table),清理數據並將數據幀傳遞給我的函數。列表數據幀與未列出的數據幀數據轉換
方式2: 我構造的數據幀(使用函數read.table)的列表,清理數據和lapply移到該列表和數據幀傳遞給我的功能。目前這個列表只包含一個項目。
我已經檢查二者方式1和方式2,二者均由之前的功能相同的數據的數據幀。我曾嘗試使用改造的數據幀路2:
lapply(DFList, function(i) {
myfunction(as.integer(as.character(i)))
})
然而,當我做到這一點我myfunction的擊穿內執行的操作。所以我想這也不是改變我的數據的適當方式。因此,我的問題是:嵌套在列表中的數據框和沒有嵌套在列表中的數據框之間有什麼區別?如果存在差異:我如何才能使嵌套數據框等於非嵌套數據框?
**編輯 - 添加的代碼**
dr <- function (df, settlement = FALSE, ret="roll", startAdj = F) {
error <- NULL
rollReturn <- NULL
monthBefore <- NULL
lastTradingDayMonthBefore <- NULL
firstTradingDay <- sapply(df, function (x) min(which(!is.na(x))))
if (settlement == FALSE) {
# First, select the number of the last trading day of every contract
lastTradingDay <- sapply(df, function (x) max(which(!is.na(x))))
n <- 1
for (i in lastTradingDay) {
monthBefore[n] <- as.character(seq(date[i], length = 2, by = "-1 months")[2])
n = n + 1
}
monthBefore <- as.Date(monthBefore) #convert character to date again
n <- 1
monthBefore <- format(monthBefore, "%Y-%m")
for (i in monthBefore) {
lastTradingDayMonthBefore[n] <- max(grep(i, date))
n <- n + 1
}
}
else {
lastTradingDayMonthBefore <- settlement
}
n <- 1
z <- 1
dataAvailableFrom <- apply(df, 2, function (x) min(which(!is.na(x)))) #select for each contract the date from which the contract has data
retMatrix <- matrix(,nrow=length(df),ncol=6)
colnames(retMatrix) <- c("previousContract", "newContract", "difference", "rollDate","startDate", "endDate")
pastRollRow <- NULL
while (z < nrow(df) && z < length(df)) {
row <- lastTradingDayMonthBefore[z]
rollFrom <- as.numeric(as.character(df[row,z]))
x <- z
z <- max(which(dataAvailableFrom <= lastTradingDayMonthBefore[z]))
rollTo <- as.numeric(as.character(df[row,z]))
#backadj
adjRatio <- rollTo/rollFrom
ts <- backAdjTSFun(ts,row,pastRollRow,x,df,adjRatio);
pastRollRow <- row
retMatrix[n,1] <- rollFrom
retMatrix[n,2] <- rollTo
retMatrix[n,3] <- calcReturn(rollFrom,rollTo)
retMatrix[n,4] <- as.character(date[row])
n <- n + 1
}
retMatrix[1,5] <- as.character(date[firstTradingDay[1]])
retMatrix[1,6] <- as.character(date[tail(date[row],1)])
if (startAdj == TRUE && ret == 'ts' || ret == 'tsReturn') {
ts <- c(rep('NA',firstTradingDay[1]-1),ts)
}
return(returnVal(ret, error, retMatrix, ts))
}
commodities <- list('FeederCattle');
path <- "~/Thesis/"
df <- lapply(commodities, function (i) {
#Load the data
i <- read.table(paste(path,i,".csv", sep = ""), sep=";", quote="\"", dec = ".", stringsAsFactors=F, strip.white=T, fill=T)
return(i)
})
#create a variable with the date range
date <- as.Date(df[[1]][,1], "%d/%m/%y")
df <- lapply(df, function(i) {
#Clean the data
i <- i[-(1:5),]
i[,1] <- NULL
x <- 1
while(x <= length(i)) {
if (!is.na(i[[1,x]])) {
i[,x] <- NULL
}
else {
x <- x + 1
}
}
return(i)
})
lapply(df, function (i) {
DR <- dr(i, ret="ts", startAdj=T)
View(DR)
}
'as.integer(字母)'可以幫助你發現問題 –
你到底是什麼意思?根據akrun的回答迴應您的評論,我之所以嘗試as.integer操作,是因爲我認爲R以不同的方式從列表中解讀數據框。我也嘗試過'as.numeric()',因爲我的數據包含包含小數的價格。但是,這些方法都沒有奏效。我也嘗試過你的建議'storage.mode(y)< - 「integer」',但R響應以下錯誤:' storage.mode(i)< - 「integer」中的錯誤: (list)object can not be coerced鍵入'整數'。 – thenoob
那麼你去了。我懷疑'as.integer'能夠做到這一點。我對你爲什麼進行這些轉換感到困惑。列表可以有許多不同的數據類型。你想壓縮它嗎?這將有助於看看你的功能 –