Hi,
I have an issue with sampling code below. Iam trying to use it with my CSV file but iam getting error. I read The csv file and save it as frame then name it df, but its not working ? The csv file are composit of ID, N1, N2 where ID are string the N1, N2 , real numbers.
Please any help ?
############### Sampling method. ##################
Systematic sampling method on a data.frame.
rw.sample <- function(df = NULL, n = NULL) {
N <- nrow(df)
int <- ceiling(N / n)
Creating an id
column.
id.creator <- function(df = NULL) {
print("Creating 'id' columns -- using a highly inefficient method.")
# Create progress bar.
pb <- txtProgressBar(min = 0, max = N, style = 3)
a <- data.frame(1)
for (i in 1:nrow(df)) {
# Update progress bar.
setTxtProgressBar(pb, i)
x <- i
a <- rbind(a, x)
}
close(pb)
a <- data.frame(a[1:nrow(a) - 1,])
df <- cbind(a, df)
colnames(df)[1] <- "id"
return(df)
}
df <- id.creator(df = df)
Function for sampling.
sampler <- function(df = NULL) {
print("Sampling.")
# Create progress bar.
pb <- txtProgressBar(min = 0, max = n, style = 3)
a <- data.frame()
x <- as.numeric(df$id[sample(nrow(df), 1)])
for (i in 1:n) {
# Update progress bar.
setTxtProgressBar(pb, i)
if (((x + int) > nrow(df)) == FALSE) {
x <- x + int
}
if (((x + int) > nrow(df)) == TRUE) {
x <- ((x + int) - nrow(df))
}
a <- rbind(a, x)
}
close(pb)
a <- data.frame(a)
colnames(a)[1] <- "id"
a <- merge(df, a)
return(a)
}
a <- sampler(df = df)
print("Done.")
return(a)
}