-1

So, I have a function:

 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
} 

Basically when you call the specific numberic id (say 4), you are suppose to get this output

 id  nobs 
  15 328 

So, I just need the nobs data to be fed into another function which measures the correlation between two columns if the nobs value is greater than another arbitrarily determined value(T). Since nobs is determined by the value of id, I am uncertain how to create a function that takes into account the output of the other function?

I have tried something like this:

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)

I have also tried to make the complete function a data.frame but it does not work and it gives me the following error: error in data.frame(... check.names = false) arguments imply differing number of rows. So, I am not sure how to proceed....

1 Answers1

0

First off, a reproducible example always helps in getting your question answered, along with a clear explanation of what your functions do/are supposed to do. We cannot run your example code.

Next, you seem to have an error in your corr function. You make multiple references to id but never actually populate this variable in your example code. So we'll just have to guess at what you need help with.

I think what you are trying to do is:

  1. given an id, call complete with that id
  2. use the nobs from that in your code.

In this case, you need to make sure to store the output of your call to complete, e.g.

comp <- complete('spec', id)

You can access the id column value comp['id'] and the nobs value via comp['nobs'] so you could do e.g.

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

Make sure you store the output of cor somewhere if you wish to actualy get it back later.

You will have to fix the problem of id not being defined yourself, because it is unclear what you want that to be.

Community
  • 1
  • 1
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194