Created a function that works fine but when asked to operate on its output it fails

Fapb88ve

New Member
Hello,

I have an assignment that requires me to create a function that calculates the correlations between two columns of 332 csv files based on my input. In short, what it does is find correlations between two columns of only the files that have either the same or more of complete cases as the threshold. My code is as follows:

corr <- function(directory, threshold = 0){
setwd(file.path(getwd(), directory))
a <- list.files(getwd())
i <- 1
while(i <= length(a)){
if(sum(as.numeric(complete.cases(b))) >= threshold & exists("f")){
tf <- cor(b[, 2], b[, 3], use = "pairwise.complete.obs")
f <- cbind(f, tf)
rm(tf)
}
if(sum(as.numeric(complete.cases(b))) >= threshold & !exists("f")){
f <- cor(b[, 2], b[, 3], use = "pairwise.complete.obs")
}
i <- i+1
}
f
setwd(...)
}

The code works fine but as part of a follow up question I'm supposed to run the following bit of code:

cr <- corr("specdata")
cr <- sort(cr)
set.seed(868)
out <- round(cr[sample(length(cr), 5)], 4)
print(out)

When I try to run the "out" bit it produces the following message:
"cannot take a sample larger than the population when 'replace = FALSE'"

Clearly the problem lies in that part but I have no idea how to solve the problem. Can anybody point me in the direction as to why this is screwing up?

I would greatly appreciate it.

JesperHP

TS Contributor
Re: Created a function that works fine but when asked to operate on its output it fai

pls do ?sample and read the following explanation:

sample takes a sample of the specified size from the elements of x using either with or without replacement.
Usage

sample(x, size, replace = FALSE, prob = NULL)

Youre code
Code:
sample(length(cr), 5)
tries to sample from length(cr) how 5 times. But length(cr) is just a single number. You cannot sample from it 5 times without replacement and in any case such a sample is uninterestingly equal to rep(length(cr),5). I doubt that is what you aim to do?