2016-05-31 45 views
-1

那么的投入,我有一个函数:输出应该是另一个功能

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)参数意味着不同的行数。所以,我不知道如何继续......

回答

0

首先,reproducible example总是有助于让您的问题得到解答,并清楚说明您的功能应该做什么。我们无法运行您的示例代码。

接下来,您似乎在您的corr函数中有错误。您对id进行了多次引用,但从未实际填充示例代码中的此变量。所以我们只需要猜测你需要什么帮助。

认为你正在尝试做的是:

  1. 给予id,调用completeid
  2. 使用不同于nobs在你的代码。

在这种情况下,您需要确保将您的呼叫输出存储到complete,例如,

comp <- complete('spec', id) 

您可以访问id列值comp['id'],并通过comp['nobs']nobs值,所以你可以做如

if (comp['nobs'] > t) { 
    # do stuff e.g. 
    cor(g.allse, g.allnit) 
} 

确保您cor输出的地方,如果你想获得actualy回来后。

你将不得不修复你自己定义的id的问题,因为它不清楚你想要的是什么。