那麼的投入,我有一個函數:輸出應該是另一個功能
complete <- function(directory,id = 1:332) {
directory <- list.files(path="......a")
g <- list()
for(i in 1:length(directory)) {
g[[i]] <- read.csv(directory[i],header=TRUE)
}
rbg <- do.call(rbind,g)
rbgr <- na.omit(rbg) #reads files and omits NA's
complete_subset <- subset(rbgr,rbgr$ID %in% id,select = ID)
table.rbgr <- sapply(complete_subset,table)
table.rbd <- data.frame(table.rbgr)
id.table <- c(id)
findla.tb <- cbind (id.table,table.rbd)
names(findla.tb) <- c("id","nob")
print(findla.tb) #creates table with number of observations
}
基本上當你調用特定的數字小ID(如4), 你想獲得這個輸出
id nobs
15 328
所以,我只需要NOBS數據被送入如果NOBS值比另一個任意確定的值(T)大,其測量兩列之間的相關性的另一功能。由於nobs是由id的值決定的,我不確定如何創建一個考慮其他函數的輸出的函數?
我已經試過這樣的事情:
corr <- function (directory, t) {
directory <- list.files(path=".......")
g <- list()
for(i in 1:length(directory)) {
g[[i]] <- read.csv(directory[i],header=TRUE)
}
rbg <- do.call(rbind,g)
g.all <- na.omit(rbg) #reads files and removes observations
source(".....complete.R") #sourcing the complete function above
complete("spec",id)
g.allse <- subset(g.all,g.all$ID %in% id,scol)
g.allnit <- subset(g.all,g.all$ID %in% id,nit)
for(g.all$ID %in% id) {
if(id > t) {
cor(g.allse,g.allnit) #calcualte correlation of these two columns if they have similar id
}
}
#basically for each id that matches the ID in g.all function, if the id > t variable, calculate the correlation between columns
}
complete("spec", 3)
cr <- corr("spec", 150)
head(cr)
我也試圖使完整功能的data.frame,但它不工作,它給了我下面的錯誤:在data.frame 錯誤(... check.names = false)參數意味着不同的行數。所以,我不知道如何繼續......