【发布时间】:2016-05-31 02:29:00
【问题描述】:
所以,我有一个函数:
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 值大于另一个任意确定的值 (T),我只需要将 nobs 数据输入另一个函数,该函数测量两列之间的相关性。由于 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) 参数中的错误意味着行数不同。所以,我不知道如何继续......
【问题讨论】: