I have the following code inside a function
Myfunc<- function(directory, MyFiles, id = 1:332) { # uncomment the 3 lines below for testing #directory<-"local" #id=c(2, 4) #MyFiles<-c(f2.csv,f4.csv) idd<-id df2 <- data.frame() for(i in 1:length(idd)) { EmptyVector <- read.csv(MyFiles[i]) comp_cases[i]<-sum(complete.cases(EmptyVector)) print(comp_cases[[i]]) id=idd[i] ret2=comp_cases[[i]] df2<-rbind(df2,data.frame(id,ret2)) } print(df2) return(df2) } This works when I try to run it in R by selecting the code inside the function and commenting out the return. I get a nice data frame like from the print statement:
> df2 id ret2 1 2 994 2 4 7112 However, when I try to return the dataframe df2 from the function it only returns the 1st row, ignoring all other values. My problem is that it works within the function for various values I have tried (opening multiple files with various combinations) and not when I try to return the data frame. Can someone help please. Thanks a lot in advance.